Python多线程导致队列不完整

时间:2016-06-30 19:00:03

标签: python multithreading

Flask应用内部,我使用multithreading模块查询两个不同的数据库表,它工作正常。就在最近,我开始注意到结果队列中的混淆。就像我通常访问结果result_queue[0]result_queue[1]一样,但我注意到result_queue[0]包含与result_queue[1]相同的结果。这不应该是因为他们正在查询两个不同的表...结果应该永远不会相同,当我重新启动Flask时它再次正常工作,一段时间结果再次变得相同,我必须重新启动

delay = 1
result_queue = []
worker1 = DatabaseWorker("db1", "select something from sometable",
        result_queue)
worker2 = DatabaseWorker("db1", "select something from othertable",
        result_queue)
worker1.start()
worker2.start()

# Wait for the job to be done
while len(result_queue) < 2:
    sleep(delay)
job_done = True
worker1.join()
worker2.join()

result0 = result_queue[0]
result1 = result_queue[1]

#result0 is showing same result as result1 until I restart Flask

0 个答案:

没有答案