推荐学习书目
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
linhanqiu
V2EX  ›  Python

请问大家 Python await(asyncio)的后面只能接一个协程吗,例如 await asyncio.sleep(5)不能是 await lambda x:x+1

  •  
  •   linhanqiu · Oct 18, 2017 · 2808 views
    This topic created in 3173 days ago, the information mentioned may be changed or developed.

    如果我想这样的话可以吗: def consumer(q): global num global server_path while True: if not q.empty(): a = q.get() cd = crawler("",a) cd["img"]=sftp_upload_file(server_path,(os.path.join(os.getcwd()+pic_download(cd["img"],num)))) num+=1 #insertinto 是一个 aiomysql 的方法,一个异步插入的方法 insertinto(cd) else: break

    6 replies    2017-10-19 07:36:42 +08:00
    ipwx
        1
    ipwx  
       Oct 18, 2017
    你可以定义一个 async def consumer(q)
    linhanqiu
        2
    linhanqiu  
    OP
       Oct 18, 2017
    @ipwx 多谢,我也刚刚想到,并且测试好了,可以这么用,不知道大神有没有用过 asyncio 加上 mulitprocessing 共用的情况
    1314258
        3
    1314258  
       Oct 18, 2017
    不能 await lambda x:x+1
    lambda 不能 awaitable。
    Kilerd
        4
    Kilerd  
       Oct 18, 2017
    @1314258 最新的 proposal 已经支持这种写法了
    NoAnyLove
        5
    NoAnyLove  
       Oct 18, 2017
    await lambda x:x+1
    没有意义啊,lambda 表达式并不是 awaitable 的,并且结构和功能简单,没有必要移交控制权。

    @Kilerd PEP-492 中倒是提了一句 Async lambda functions。有新的 PEP 了吗?编号是多少?
    janxin
        6
    janxin  
       Oct 19, 2017
    @NoAnyLove 应该还只是 proposal,没有实现
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   961 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 22:47 · PVG 06:47 · LAX 15:47 · JFK 18:47
    ♥ Do have faith in what you're doing.