问题
Ok, so this may be a little bit unorthodox or I'm just stupid or both :)
I'm trying a very simple setup where I start a bottle server in one Process
instance and start a smallish TFTP server in another instance.
#!/usr/bin/env python
import bottle
import sys
import tftpy
from multiprocessing import Process
def main():
try:
t = Process(target=bottle.run(host='0.0.0.0', port=8080))
t.daemon = True
t.start()
t.join()
h = Process(target=tftpy.TftpServer('/srv/tftp').listen('0.0.0.0', 69))
h.start()
h.join()
except KeyboardInterrupt:
sys.stdout.write("Aborted by user.\n")
sys.exit(1)
if __name__ == "__main__":
main()
Unless I'm totally crazy, I'd expect them to start up in parallel. In reality, what happens is that bottle starts and locks whole thing up. If I exit bottle, TFTP daemon starts.
I also tried a similar approach with threading
module, with about same results.
What am I doing wrong?
回答1:
There are several issues:
you call
run()
in the main thread. You should pass arguments inargs
instead:Process(target=bottle.run, kwargs=dict(host='0.0.0.0', port=8080))
you call t.join() which blocks until t process ends before h.start(). Join after all processes are started instead
- bottle, tftpy might not be compatible with multiprocessing module. You could try subprocess module if so
回答2:
Well, I am not sure if I understood what you are trying to accomplish but if I were in your place I would try to use the python-daemon package
I think that both bottle
and TFTP
could be daemonized.
As you are only in search for a simple test I guess that the examples given in the python-daemon webpage would be enough.
If you really like the idea of going daemonizing things, I would suggest also that you search about the proper daemonizing approach for your platform as this way you have several facilities to manage your daemon by making them more alike to the ones found in your OS.
For some simple examples: http://troydhanson.wordpress.com/2012/08/21/minimal-sysvinit-launchd-and-upstart/
来源:https://stackoverflow.com/questions/13021762/starting-python-bottle-in-a-thread-process-and-another-daemon-next-to-it