看看thread.join()
方法。基本上它会阻止你的调用线程,直到子线程返回(从而阻止它在它应该之前退出)。
更新:
为了避免你的主线程没有反应,你可以使用while循环的新要求。
while threading.active_count() > 0:
# ... look for new requests to handle ...
time.sleep(0.1)
# or try joining your threads with a timeout
#for thread in my_threads:
# thread.join(0.1)
更新2:
它也像thread.start_new(func, args)
是obsolete。它被更新为thread.start_new_thread(function, args[, kwargs])
您还可以创建线程与更高级别的线程包(这是一个让您得到在前面的代码块中的active_count()
包):
import threading
my_thread = threading.Thread(target=func, args=(), kwargs={})
my_thread.daemon = True
my_thread.start()
这可能会起作用,但是当以FastCGI的方式运行时,是不是会阻止脚本处理新的请求? – crgwbr 2010-05-17 16:17:40
我从来没有使用FastCGI,但可能。你可以做的是把连接放在一个while循环中,并给它一个超时,所以它试图加入线程,超时并寻找新的请求。 – tgray 2010-05-17 16:33:28
将它设置为一个守护进程看起来好像会起作用。我没有意识到'start_new'已被弃用。 谢谢 – crgwbr 2010-05-17 17:33:29