Using a background thread with asyncio/futures with flask

Frank Millman frank at chagford.com
Fri Mar 22 07:28:56 EDT 2024


On 2024-03-22 1:23 PM, Frank Millman via Python-list wrote:
> On 2024-03-22 12:09 PM, Frank Millman via Python-list wrote:
>>
>> I am no expert. However, I do have something similar in my app, and it 
>> works.
>>
>> I do not use 'await future', I use 'asyncio.wait_for(future)'.
>>
> 
> I tested it and it did not work.
> 
> I am not sure, but I think the problem is that you have a mixture of 
> blocking and non-blocking functions.
> 
> Here is a version that works. However, it is a bit different, so I don't 
> know if it fits your use case.
> 
> I have replaced the threads with background asyncio tasks.
> 
> I have replaced instances of queue.Queue with asyncio.Queue.
> 
> Frank
> 
> ===============================================
> 
> import asyncio
> 
> in_queue = asyncio.Queue()
> out_queue = asyncio.Queue()
> 
> async def worker():
>      print("worker started running")
>      while True:
>          future = await in_queue.get()
>          print(f"worker got future: {future}")
>          await asyncio.sleep(5)
>          print("worker sleeped")
>          await out_queue.put(future)
> 
> async def finalizer():
>      print("finalizer started running")
>      while True:
>          future = await out_queue.get()
>          print(f"finalizer got future: {future}")
>          future.set_result("completed")
>          print("finalizer set result")
> 
> async def main():
>      asyncio.create_task(worker())  # start a background task
>      asyncio.create_task(finalizer())  # ditto
>      future = asyncio.get_event_loop().create_future()
>      await in_queue.put(future)
>      print(f"main put future: {future}")
>      result = await asyncio.wait_for(future, timeout=None)
>      print(result)
> 
> if __name__ == "__main__":
>      # loop = asyncio.get_event_loop()
>      # loop.run_until_complete(main())
> 
>      # this is the preferred way to start an asyncio app
>      asyncio.run(main())
> 
> 

One more point.

If I change 'await asyncio.wait_for(future, timeout=None)' back to your 
original 'await future', it still works.



More information about the Python-list mailing list