推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Te11UA
V2EX  ›  Python

Python 协程里 for 很长的 list 会阻塞吗?

  •  
  •   Te11UA · Oct 30, 2020 · 2235 views
    This topic created in 2048 days ago, the information mentioned may be changed or developed.
    由于看到有 `async for` 和 `for await `,所以不清楚如果 task_list 是一个比较长的 list 是否会阻塞呢?找了一圈谷歌没有说到这个的...

    ```python
    async def func():
    ____task_list = list()
    ____for task in tasks.task_list:
    ________status= await redis_client.get(f'task:{task}')
    ```
    2 replies    2020-10-31 04:27:16 +08:00
    fasionchan
        1
    fasionchan  
       Oct 30, 2020
    不会,如果子任务阻塞的话,await 会让出执行权
    black11black
        2
    black11black  
       Oct 31, 2020
    取决于你调用方法内部是否有把控制权返回的命令。一个异步函数,内部如果没有任何异步调用,过程中即使 await 也是不会向 eventloop 返回的,也就是说执行时阻塞。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5485 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:18 · PVG 15:18 · LAX 00:18 · JFK 03:18
    ♥ Do have faith in what you're doing.