Re: Using a background thread with asyncio/futures with flask

Liste des GroupesRevenir à cl python 
Sujet : Re: Using a background thread with asyncio/futures with flask
De : frank (at) *nospam* chagford.com (Frank Millman)
Groupes : comp.lang.python
Date : 22. Mar 2024, 12:28:56
Autres entêtes
Message-ID : <mailman.11.1711106969.3468.python-list@python.org>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
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.

Date Sujet#  Auteur
22 Mar 24 o Re: Using a background thread with asyncio/futures with flask1Frank Millman

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal