I'm trying to run a while True
loop with asyncio.
import asyncio
async def cpu_bound_task():
something that take long time
return result
async def post_result_from_cpu_bound_task(res):
_loop = asyncio.get_event_loop()
post_res = await _loop.run_in_executor(
None,
request.post,
end_point,
data
)
async def main():
while True:
cpu_task = asyncio.create_task(cpu_bound_task())
res = await cpu_task
post_result_task = asyncio.create_task(post_result_from_cpu_bound_task(res))
r = await post_result_task
asyncio.run(main())
What I expect is
- do cpu task
- and it return result as res
- pass res to coroutine that post res
- while posting the res do cpu task asynchronously
- when cpu task is done, the post task that run asynchronously with cpu task would be done too
- go to phase 2
but its actual workflow is
- do cpu task
- and it return result as res
- pass res to coroutine that post res
- post task is done
- go to phase 1
The Python version I'm using is 3.10.