CARVIEW |
Relinquishing control in Python asyncio
asyncio
in Python is a form of co-operative multitasking, where everything runs in a single thread but asynchronous tasks can yield to other tasks to allow them to execute.
Normally you do this with await
- but I'm thinking through a problem at the moment which could involve long-running asyncio functions. To avoid blocking the event loop, I'd like them to periodically yield to see if there are any other tasks that need to spend some time with the CPU.
This doesn't seem to be covered in the Python asyncio
documentation, but after some digging I came across this issue in the old python/asyncio
repo: Question: How to relinquishing control to the event loop in Python 3.5
It turns out the supported, optimized idiom is this one:
await asyncio.sleep(0)
Related
- python Running Python code in a subprocess with a time limit - 2020-12-06
- selenium Using async/await in JavaScript in Selenium - 2020-10-02
- json Processing a stream of chunks of JSON with ijson - 2023-08-15
- python Checking if something is callable or async callable in Python - 2023-08-04
- python Simple load testing with Locust - 2022-10-22
- awslambda Deploying Python web apps as AWS Lambda functions - 2022-09-18
- pytest Async fixtures with pytest-asyncio - 2022-03-19
- gpt3 Using the ChatGPT streaming API from Python - 2023-04-01
- deno Running Python code in a Pyodide sandbox via Deno - 2023-05-10
- llms A simple Python implementation of the ReAct pattern for LLMs - 2023-03-17
Created 2020-12-29T11:25:14-08:00 · Edit