Re: calling a function asynchronously

2022-03-30 Thread Kirill Ratkin via Python-list
Hi 30.03.2022 21:44, Larry Martell пишет: On Wed, Mar 30, 2022 at 2:40 PM Kirill Ratkin via Python-list 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)

Re: calling a function asynchronously

2022-03-30 Thread Larry Martell
On Wed, Mar 30, 2022 at 2:40 PM Kirill Ratkin via Python-list 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") > > >

Re: calling a function asynchronously

2022-03-30 Thread Kirill Ratkin via Python-list
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')

Re: calling a function asynchronously

2022-03-30 Thread Kirill Ratkin via Python-list
Hi, You can use asyncio.create_task and gather results. See docs - https://docs.python.org/3/library/asyncio-task.html But think twice what you want to do in async task. Do you use synchronous requests to database? If yes it will blocks eventloop... If you use Django it makes sense to use

calling a function asynchronously

2022-03-30 Thread Larry Martell
I have a django app, and for a certain request I need to kick off a long running task. I want to do this asynchronously and immediately return a response. I tried using subprocess.Process() but the forked process does not have a django database connection. I then tried posting a request using ajax