我正在尝试使用Websockets和asyncio在Python中创建一个脚本,以侦听多个套接字。问题是无论我做什么,它只会侦听我调用的第一个套接字。我认为这是由于无限循环造成的,解决此问题的选项是什么?为每个套接字使用线程?
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)
subscribe_all
... - user4815162342test()
函数? - user4815162342