calling a function asynchronously

Larry Martell larry.martell at gmail.com
Wed Mar 30 14:44:40 EDT 2022


On Wed, Mar 30, 2022 at 2:40 PM Kirill Ratkin via Python-list
<python-list at python.org> wrote:
>
> 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 ...

Appreciate the reply. I did not know about django-background-tasks -
thanks. I've been trying to make use of that. I do not get the errors
I was getting before but it does not appear that my long running task
is running at all. Still debugging. But concerting asyncio - doesn't
run_until_complete block until long() completes?

>
> 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')
> --
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list