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
linhanqiu
V2EX  ›  Python

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

  •  
  •   linhanqiu · 2017-10-18 22:21:40 +08:00 · 2287 次点击
    这是一个创建于 2378 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    @Kilerd PEP-492 中倒是提了一句 Async lambda functions。有新的 PEP 了吗?编号是多少?
    janxin
        6
    janxin  
       2017-10-19 07:36:42 +08:00
    @NoAnyLove 应该还只是 proposal,没有实现
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3280 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:28 · PVG 21:28 · LAX 06:28 · JFK 09:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.