【发布时间】:2014-07-27 17:00:57
【问题描述】:
我已经在 Python 文件 myfile.py 中成功实现了线程 + 队列。现在我希望这个文件作为守护进程运行,因为当所有线程都完成了它们的任务时,我想重新填充队列并让线程处理新任务。我在这里尝试了一些代码,但程序没有正确响应:
# myfile.py
threadList = list()
i = 0
while i < 10:
i += 1
threadName = "T" + str(i)
threadList.append(threadName)
#create queue
myQueue = Queue.Queue();
# create thread objects
threads = list()
for threadName in threadList:
thread = WorkerThread(threadName, myQueue)
thread.start()
threads.append(thread)
def hello():
while True:
logger.debug("true")
if myQueue.empty():
logger.debug("empty")
else:
logger.debug("not empty")
def run():
daemon_context = daemon.DaemonContext(files_preserve=[handler.stream],
stdout = open("./stdout.log","wb"),
stderr = open("./stderr.log","wb"))
with daemon_context:
hello()
if __name__ == "__main__":
run()
当脚本执行时,它会打印“true”并停在那里。它不会记录“空”或“非空”。终端和 stderr.log 中没有显示错误。但是,如果我删除myQueue.empty() 的条件检查,守护程序将继续打印“true”。为什么队列在守护进程中不起作用?
【问题讨论】:
-
您是否已通过调试器?如果是这样,请在
logger.debug("true")上休息并查看状态。采取下一步,然后看看 myQueue.empty() 正在评估什么——我已经看到标准 lib 代码会爆炸、静默失败、永远不会评估,因此永远不会进入条件评估的实例。更好的是,对于“腰带和吊带”方法,将您的if块包装在try/except中,看看是否抛出异常。这不会解决你的问题,但它会帮助你调试这里发生的事情。
标签: python multithreading python-multithreading python-daemon