V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-10-30 11:41:40 +08:00 · 1792 次点击
    这是一个创建于 1281 天前的主题,其中的信息可能已经有所发展或是发生改变。
    由于看到有 `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 条回复    2020-10-31 04:27:16 +08:00
    fasionchan
        1
    fasionchan  
       2020-10-30 13:11:12 +08:00
    不会,如果子任务阻塞的话,await 会让出执行权
    black11black
        2
    black11black  
       2020-10-31 04:27:16 +08:00
    取决于你调用方法内部是否有把控制权返回的命令。一个异步函数,内部如果没有任何异步调用,过程中即使 await 也是不会向 eventloop 返回的,也就是说执行时阻塞。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   795 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:07 · PVG 05:07 · LAX 14:07 · JFK 17:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.