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

前端 未结 2 1753
名媛妹妹
名媛妹妹 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:19

    As I see from the traceback, we only reach Step 2 of 4

    From the asyncio docs:
    This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources

    So I drop my first proposal using multithreading.
    I could imagin the following three options:

    1. Do it with multiprocessing instead of multithreading
    2. Do it with coroutine inside asyncio loop
    3. Switch between channels in def onJoin(self, details)

    Second proposal, first option using multiprocessing.
    I can start two asyncio loops, so appRunner.run(...) should work.

    You can use one class ApplicationSession if the channel are the only different. If you need to pass different class ApplicationSession add it to the args=

    class __ApplicationSession(ApplicationSession):
            # ...
            try:
                yield from self.subscribe(onTicker, self.config.extra['channel'])
            except Exception as e:
                # ...
    
    import multiprocessing as mp
    import time
    
    def ApplicationRunner_process(realm, channel):
            appRunner = ApplicationRunner("wss://api.poloniex.com:443", realm, extra={'channel': channel})
            appRunner.run(__ApplicationSession)
    
    if __name__ == "__main__":
        AppRun = [{'process':None, 'channel':'BTC_LTC'},
                  {'process': None, 'channel': 'BTC_XMR'}]
    
        for app in AppRun:
            app['process'] =  mp.Process(target = ApplicationRunner_process, args = ('realm1', app['channel'] ))
            app['process'].start()
            time.sleep(0.1)
    
        AppRun[0]['process'].join()
        AppRun[1]['process'].join()
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题