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」を出力します。キューがデーモンで機能しないのはなぜですか?