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

前端 未结 2 1765
名媛妹妹
名媛妹妹 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()
    

提交回复
热议问题