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