0

Inside a Flask app, I'm using multithreading module to query two different database tables and it works fine. Just recently I started noticing mix up in the result queue. Like I normally access the results like this result_queue[0] and result_queue[1] but I noticed result_queue[0]contains same result as result_queue[1]. This shouldn't be because they are querying two different tables...results should never be the same and when I restart Flask it works fine again and after a while results becomes same again and I have to restart

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
ArchieTiger
  • 2,083
  • 8
  • 30
  • 45
  • This is a follow up question on: http://stackoverflow.com/questions/37975904/how-to-use-python-to-query-database-in-parallel/37976284#37976284 – ArchieTiger Jun 30 '16 at 19:01
  • @ArchieTiger , can you clarify what you mean by the results becoming the same? Does result[0] start looking like what result[1] normally looks like? Or do you mean that after you run it, result[0] == result[1]? – Zachary Jacobi Jun 30 '16 at 21:04
  • @ZacharyJacobi, yes after I ran it, `result[0] == result[1]` – ArchieTiger Jun 30 '16 at 21:47
  • @ArchieTiger: Can you try changing your results queue to a dictionary and passing a key in with each worker when you create it? I'm wondering if there's some sort of race condition. Let's say you call the endpoint twice. Maybe one of the results is returning twice before the other has a chance to return once? I'm strongly betting on a race condition being the culprit, but I'm not sure where exactly it could be. – Zachary Jacobi Jul 04 '16 at 19:56

0 Answers0