我试图同时在python中运行两个autobahn.asyncio.wamp.ApplicationSession
。以前,我使用了对autobahn库的修改,如this post's answer中所建议的那样。我现在
需要更专业的解决方案。在
在搜索了一段时间后,this post appeared quite promising,但是使用了twisted
库,而不是{autobahn
库的asyncio
分支找到类似的解决方案,因为它似乎没有使用Reactors
。在
我的主要问题是ApplicationRunner.run()
是阻塞的(这就是我之前将它外包给线程的原因),所以我不能在它之后运行第二个ApplicationRunner
。在
我确实需要同时访问2个websocket频道,这似乎不能用一个ApplicationSession
来完成。在
目前我的代码:
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine
import time
channel1 = 'BTC_LTC'
channel2 = 'BTC_XMR'
class LTCComponent(ApplicationSession):
def onConnect(self):
self.join(self.config.realm)
@coroutine
def onJoin(self, details):
def onTicker(*args, **kwargs):
print('LTCComponent', args, kwargs)
try:
yield from self.subscribe(onTicker, channel1)
except Exception as e:
print("Could not subscribe to topic:", e)
class XMRComponent(ApplicationSession):
def onConnect(self):
self.join(self.config.realm)
@coroutine
def onJoin(self, details):
def onTicker(*args, **kwargs):
print('XMRComponent', args, kwargs)
try:
yield from self.subscribe(onTicker, channel2)
except Exception as e:
print("Could not subscribe to topic:", e)
def main():
runner = ApplicationRunner("wss://api.poloniex.com:443", "realm1", extra={})
runner.run(LTCComponent)
runner.run(XMRComponent) # <- is not being called
if __name__ == "__main__":
try:
main()
except keyboardInterrupt:
quit()
except Exception as e:
print(time.time(), e)
我对autobahn
库的了解是有限的,恐怕文档并不能改善我的状况。我是不是在看什么?一个函数,一个参数,它能让我组合我的组件或者同时运行它们?在
也许是一个类似于provided here的解决方案,它实现了一个可选的ApplicationRunner
?在
相关主题
Running two ApplicationSessions in twisted
Running Autobahn ApplicationRunner in Thread
Autobahn.wamp.ApplicationSession Source
Autobahn.wamp.Applicationrunner Source
根据要求,使用multithreading
代码从@stovfl的答案进行回溯:
正如我从
traceback
中看到的,我们只到达第2步,共4步因此,我放弃了我的第一个建议使用
multithreading
。我可以想象以下三种选择:
multiprocessing
代替multithreading
coroutine
在asyncio loop
内完成def onJoin(self, details)
中的channels
之间切换第二个建议,第一个选项使用
multiprocessing
。我可以启动两个
asyncio loops
,所以appRunner.run(...)
应该可以工作。在如果
channel
是唯一不同的,那么可以使用一个class ApplicationSession
。 如果需要传递不同的class ApplicationSession
,请将其添加到args=
按照这个方法you linked for twisted我设法用asyncio settingstart_loop=False获得相同的行为
相关问题 更多 >
编程相关推荐