Running several ApplicationSessions non-blockingly using autbahn.asyncio.wamp

前端 未结 2 1766
名媛妹妹
名媛妹妹 2021-01-06 04:44

I\'m trying to run two autobahn.asyncio.wamp.ApplicationSessions in python at the same time. Previously, I did this using a modification of the autobahn library

2条回答
  •  感情败类
    2021-01-06 05:33

    Following the approach you linked for twisted I managed to get same behaviour with asyncio setting start_loop=False

    import asyncio
    from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
    
    runner1 = ApplicationRunner(url, realm, extra={'cli_id': 1})
    coro1 = runner1.run(MyApplicationSession, start_loop=False)
    
    runner2 = ApplicationRunner(url, realm, extra={'cli_id': 2})
    coro2 = runner2.run(MyApplicationSession, start_loop=False)
    
    asyncio.get_event_loop().run_until_complete(coro1)
    asyncio.get_event_loop().run_until_complete(coro2)
    asyncio.get_event_loop().run_forever()
    
    class MyApplicationSession(ApplicationSession):
    
        def __init__(self, cfg):
            super().__init__(cfg)
            self.cli_id = cfg.extra['cli_id']
    
       def onJoin(self, details):
            print("session attached", self.cli_id)
    

提交回复
热议问题