Cooperative yield in asyncio

爷,独闯天下 提交于 2019-12-13 02:38:18

问题


I'm running a long CPU-intensive task on a (tornado) webserver. I don't want to offload this task to a thread (for now). How do I correctly "yield" control during this long-running task to the eventloop, so that web requests are still being served (note: I'm using "yield" in a cooperative-scheduling sense here, not in a generator-sense, or the python keyword yield).

My suggestion would be to do an await asyncio.sleep(0), however is there a guarantee that this is not just implemented as a NOP? I've been looking for a dedicated function in asyncio library to do this, but so far have been unable to find one.

Example (python 3.5 async/await style):

async def long_task():
    for i in range(LARGE_NUMBER):
        do_something(i)
        if i % 100 == 0:
            await asyncio.sleep(0)

回答1:


It doesn't mentioned in doc, but currently asyncio.sleep(0) is common way to do this. You can read full discussion here. Moreover asyncio.sleep(0) was specially optimized to do job. Since this case has test, I think you might not afraid about behavior.



来源:https://stackoverflow.com/questions/36647825/cooperative-yield-in-asyncio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!