Task was destroyed but it is pending for each request

Viewed 276

I am starting a new loop in each requests and I am getting below error when i manually kill my service.

ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending name='Task-192' coro=<AsyncioContextBase._run() running at C:\ProgramData\Anaconda3\lib\site-packages\caio\asyncio_base.py:54> wait_for=<Future cancelled>>

Here is my code:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
c = doSomething( maxtasks=100)
loop.run_until_complete(c.run())
try:
loop.stop()
loop.close()
print('done:')
except RuntimeError:
print('errr:')  

I have also tried to get all the pending tasking before closing the loop using below code so that I can close all the tasks before closing but I am getting "RuntimeError: no running event loop"

pending = asyncio.all_tasks()

Can anybody tell me how do I kill all these tasks after my loop ends. I dont want these loops to run in background and take my whole memory

1 Answers

Assuming you are using caio in doSomething(). This worked for me with caio:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
context = caio.AsyncioContext(16, loop=loop)
loop.run_until_complete(main(context))
context.close()
Related