calling a function asynchronously

Kirill Ratkin kirill.ratkin at devoteam.com
Wed Mar 30 14:36:09 EDT 2022


Hi again,

I changed a bit your example and it works as you expected I hope.

import asyncio


async def long():
     for i in range(100):
         await asyncio.sleep(10)
         print("long is done")


loop = asyncio.get_event_loop()

task = loop.create_task(long())
print('after asyncio.run')
loop.run_until_complete(asyncio.gather(task))


But how I wrote before ... if you are in big Django project just look at 
existent django libraries for long task running. One of it I sent you. 
There is 'celery' as well.

It's more pragmatic way ...

30.03.2022 19:10, Larry Martell пишет:
> import asyncio
> import time
>
> async def long():
>      for i in range(100):
>         time.sleep(10)
>
> asyncio.run(long())
> print('after asyncio.run')


More information about the Python-list mailing list