python协程asyncio 应用_Python-如何使用asyncio同时运行多个协程?

TL;DR使用^{}同时运行多个协程。Maybe this scenario requires a framework based on events/callbacks rather than one based on coroutines? Tornado?

不,你不需要其他框架。异步应用程序与同步应用程序的整体思想是,在等待结果时,它不会阻塞。不管它是如何实现的,使用协程或回调。I mean, because connection_handler is constantly waiting for incoming messages, the server can only take action after it has received a message from the client, right? What am I missing here?

在同步应用程序中,您将编写类似msg = websocket.recv()的内容,这将阻塞整个应用程序,直到您收到消息为止(如您所述)。但在异步应用程序中则完全不同。

当你做msg = yield from websocket.recv()时,你会说:暂停connection_handler()的执行,直到websocket.recv()产生某些结果。在coroutine中使用yield from将控制返回到事件循环,以便在等待websocket.recv()的结果时可以执行其他一些代码。请参阅documentation以更好地了解协同工作的工作原理。Let's say we – additionally – wanted to send a message to the client whenever some event happens. For simplicity, let's send a message periodically every 60 seconds. How wou

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值