在线程/进程及其旁边的另一个守护进程中启动python瓶 [英] Starting python bottle in a thread/Process and another daemon next to it
问题描述
好吧,所以这可能有点不合常规,或者我只是愚蠢,或者两者都是:)
Ok, so this may be a little bit unorthodox or I'm just stupid or both :)
我正在尝试一种非常简单的设置,即在一个Process
实例中启动一瓶服务器,在另一个实例中启动一个较小的TFTP服务器.
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()
除非我完全疯了,否则我希望它们能够并行启动.实际上,发生的事情是瓶子启动并把整个东西锁起来.如果退出瓶子,则TFTP守护程序启动.
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.
我还尝试了使用threading
模块的类似方法,结果大致相同.
I also tried a similar approach with threading
module, with about same results.
我在做什么错了?
推荐答案
有几个问题:
-
您在主线程中调用
run()
.您应该在args
中传递参数:
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))
您调用t.join(),直到t进程在h.start()之前结束为止.在所有进程开始后加入
you call t.join() which blocks until t process ends before h.start(). Join after all processes are started instead
这篇关于在线程/进程及其旁边的另一个守护进程中启动python瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!