Python中await(asyncio)后面只能接一个协程吗?例如await asyncio.sleep(5)不能是await lambda x:x+1

如果我想这样的话可以吗: 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


Python中await(asyncio)后面只能接一个协程吗?例如await asyncio.sleep(5)不能是await lambda x:x+1

7 回复

你可以定义一个 async def consumer(q)


我无法理解你的问题

多谢,我也刚刚想到,并且测试好了,可以这么用,不知道大神有没有用过 asyncio 加上 mulitprocessing 共用的情况

不能 await lambda x:x+1
lambda 不能 awaitable。

最新的 proposal 已经支持这种写法了

await lambda x:x+1
没有意义啊,lambda 表达式并不是 awaitable 的,并且结构和功能简单,没有必要移交控制权。

PEP-492 中倒是提了一句 Async lambda functions。有新的 PEP 了吗?编号是多少?

应该还只是 proposal,没有实现

回到顶部