Reputation: 276
The multiprocessing module in Python 3.2.1 on Windows 7 x86 seems to be defeating me.
I have two modules: servmain.py and sslserver.py. The idea is to (eventually) code an application which will communicate with clients using SSL. This is the part I already have down. However, I need the server listener to be spun off into its own separate process so the main process can do some other stuff. As a dummy test, I told the child process to print "Hello World" to stdout and write text to a non-existent text file.
Here is the code for my parent process(servmain.py):
from multiprocessing import Process
import logging
if __name__ == "__main__":
logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.DEBUG)
#Fire up the server
listenerProcess = Process(target = sslserver.startServer)
listenerProcess.start()
logger.debug("Starting listener.")
listenerProcess.join()
logger.debug("Done.")
And here is the sslserver.py code:
def startServer():
print("Hello World")
f= open("testfile.txt", "w")
f.write("Hello world\n")
f.close()
When I run servmain.py, I get the following output:
[DEBUG/MainProcess] Starting listener. [DEBUG/MainProcess] Done.
Which is what I would expect. However, testfile.txt has not been created, and there is no output to stdout. Does anyone have an idea about why this might be happening?
Upvotes: 0
Views: 842
Reputation: 1529
I think I am missing a few libraries here so I had to remove your logger code because it was a problem for me. To me it would seem that you have a naming/pathing conflict. Please be sure the name 'sslserver' does not collide with any modules in python path.
Also, set python path! In my example both of these files are in the same directory.
pytest.py
#!/usr/bin/env python
from multiprocessing import Process
import sslserver
import logging
if __name__ == "__main__":
#logger = multiprocessing.log_to_stderr()
#logger.setLevel(logging.DEBUG)
#Fire up the server
listenerProcess = Process(target = sslserver.startServer)
listenerProcess.start()
#logger.debug("Starting listener.")
print "Starting Listener\n"
listenerProcess.join()
#logger.debug("Done.")
print "Done\n";
sslserver.py
#!/usr/bin/env python
def startServer():
print("Hello World")
f= open("testfile.txt", "w")
f.write("Hello world\n")
f.close()
Output
nynex@citadel:~/temp$ ./pytest.py
Starting Listener
Hello World
Done
nynex@citadel:~/temp$ cat testfile.txt
Hello world
Upvotes: 1