【发布时间】:2018-04-16 13:02:35
【问题描述】:
我正在尝试在 python 中创建一个脚本,该脚本使用 websockets 和 asyncio 侦听多个套接字,问题是无论我做什么,它只听我调用的第一个套接字。 我认为它是无限循环,我有什么选择来解决这个问题?为每个套接字使用线程?
async def start_socket(self, event):
payload = json.dumps(event)
loop = asyncio.get_event_loop()
self.tasks.append(loop.create_task(
self.subscribe(event)))
# this should not block the rest of the code
await asyncio.gather(*tasks)
def test(self):
# I want to be able to add corotines at a different time
self.start_socket(event1)
# some code
self.start_socket(event2)
【问题讨论】:
标签: python websocket python-asyncio