I am using Flask to provide a web API.
When I handle web API request(
api_Log()), I want to store some data into db. but I do not want to block web API return, so I want to store data into db as a async way.
So I put data into a Queue, and in another threading to get data from Queue.
worker is thread function.
When running, I can get
put queue size, 1, but
worker queue size, 0, it seems in thread, it can not get queue, so there is no data store into database.
I copy these code to a single py file, and run, it can works fine.
What is the problem? Thanks in advance.
taskq = Queue.Queue() def api_Log(id): data = id taskq.put(data) print 'put queue size', taskq.qsize() return 1 def worker(work): while True: try: print 'worker queue size', taskq.qsize() task = taskq.get(True, 1) print 'Get Task' print 'Store data into database' except Queue.Empty: # There is no task now, wait or exit... print 'Wait for task...' time.sleep(3)