I'm having a small web server that that i'm trying to run async functions on web request:
from aiohttp import web
import asyncio
async def funcB():
while True:
print("running")
async def funcA(req):
print('start')
asyncio.Task(funcB())
print('execute task completed')
return web.Response(text="OK Start");
if __name__ == '__main__':
app = web.Application()
app.add_routes([web.get('/', funcA)])
web.run_app(app, host='127.0.0.1', port=2000)
Once running the app and triggering it by curl http://127.0.0.1:2000/ i do get the execute task completed logline, but the curl response of the curl is not been received, if I comment out the asyncio.Task(funcB()) line - I do get the response of the CURL command - I must say that funcB dose run..
what do I miss here?
asyncio.Task(funcB())doesn’t actually run your task, it just creates aTaskobject. That’s why your problem can’t be reproduced. That said, your actual problem is thatfuncBnever yields back to the event loop. Either it needs to await something they does, or you should use a separate thread or process, depending on whether it’s I/O or CPU bound.