[英]Python: Combination of cmd and asyncio (for WAMP / autobahn)
我正在尝试为WAMP应用程序实现一个简单的命令行界面。
对于WAMP实施,使用了autobahn
python软件包。
我想要一个交互式外壳,所以我决定使用cmd
模块来解析输入。 不幸的是,我无法将autobahn
的asyncio
特性与cmd
循环结合起来。
因此,总的来说,我想拥有的东西与此类似:
import argparse
import autobahn.asyncio.wamp as wamp
import cmd
class Shell(cmd.Cmd):
intro = 'Interactive WAMP shell. Type help or ? to list commands.\n'
prompt = '>> '
def __init__(self, caller, *args):
super().__init__(*args)
self.caller = caller
def do_add(self, arg):
'Add two integers'
a, b = arg.split(' ')
res = self.caller(u'com.example.add2', int(a), int(b))
res = res.result() # this cannot work and yields an InvalidStateError
print('call result: {}'.format(res))
class Session(wamp.ApplicationSession):
async def onJoin(self, details):
Shell(self.call).cmdloop()
def main(args):
url = 'ws://{0}:{1}/ws'.format(args.host, args.port)
print('Attempting connection to "{0}"'.format(url))
try:
runner = wamp.ApplicationRunner(url=url, realm=args.realm)
runner.run(Session)
except OSError as err:
print("OS error: {0}".format(err))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('realm', type=str)
parser.add_argument('host', type=str)
parser.add_argument('port', type=int)
main(parser.parse_args())
这显然是行不通的,因为将来调用result()
时结果还没有准备好,但是由于Shell本身不是async
,所以我不能使用await。
我已经找到asynccmd
但是我asynccmd
如何在autobahn
上使用它的问题,并且总体上,我仍然对asyncio
的内部结构有点不知所措。
使用一个简单的循环
try:
while(True):
a = int(input('a:'))
b = int(input('b:'))
res = await self.call(u'com.example.add2', a, b)
print('call result: {}'.format(res))
except Exception as e:
print('call error: {0}'.format(e))
内部的onJoin
函数可以很好地工作,所以我觉得我的问题也必须有一个简单而精益的解决方案。
我们欢迎所有的建议!
事实证明,已经有解决此问题的方法。
autobahn
有两个版本,一个使用aysncio
,另一个使用来自twisted
异步回调。
crochet
程序包允许使用同步上下文中的twisted
回调,因此提供了一种解决方案。
软件包autobahn-sync
是crochet
包装器,用于autobahn,可从cmd.Cmd
(或其他任何地方)内调用RCP变得很简单:
import autobahn_sync
autobahn_sync.run(url='example.com', realm='myrealm')
autobahn_sync.call(u'com.example.add2', a, b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.