pythonpython-asyncioautobahnwamp-protocolpython-cmd

Python: Combination of cmd and asyncio (for WAMP / autobahn)


Overview

I am trying to implement a simple command-line interface for a WAMP application.

For the WAMP implementation, the autobahn python package is used.

I would like to have an interactive shell so I decided to use the cmd module to parse the input. Unfortunately, I have not been able to combine the asyncio nature of autobahn with the cmd loop.

Code

So in general what I would like to have is something similar to this:

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())

This obviously can not work since the result is not ready when result() is called on the future, but I can not use await since the Shell is not async itself.

Solution Attempts

I have found asynccmd but I could not work out how to use it with autobahn and I am in general still a bit overwhelmed by the internals of asyncio.

Using a simple loop

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))

inside the onJoin function works perfectly fine, so I feel like there has to be a simple and lean solution for my problem as well.

Any suggestions would be much appreciated!


Solution

  • It turns out that there already is a solution for this problem.

    autobahn has two versions, one using aysncio and one using asynchronous callbacks from twisted.

    The package crochet allows the usage of twisted-callbacks from a synchronous context and therefore offers a solution.

    Simple Solution

    The package autobahn-sync is a crochet-wrapper for autobahn and makes calling an RCP from within cmd.Cmd (or anywhere else) trivial:

    import autobahn_sync
    
    autobahn_sync.run(url='example.com', realm='myrealm')
    autobahn_sync.call(u'com.example.add2', a, b)