Is cmd.Cmd.cmdloop() integration with asyncio server possible?

Benjamin Risher brisher777 at gmail.com
Mon Dec 8 13:27:02 EST 2014


I'm working on an asyncio server project.  I'd also like to have a cmd.Cmd style command loop interface for spawning instances of the server.  As far as I've seen, running an asyncio server requires

...
loop.run_forever()
...

And cmd.Cmd.cmdloop() is a blocking loop, so I'm not able to call them sequentially.  Is there a way to get the cmdloop to run asynchronously?  Or a way to call asyncio.streams.start_server from the cmdloop and actually have it run continuously?  I've tried a few things but nothing has been successful.  I should mention that by itself, the server functions.

For brevity, I'll try to keep the code to what is relevant.  

class MyServer:
    ...
    def start():
        server = asyncio.async(
                    asyncio.streams.start_server(
                        self.accept_client,
                        host=self.ip,
                        port=self.port,
                        loop=self.loop
                    )
                )
        return server
    ...

class MyMenu(cmd.Cmd):
    def __init__(self, loop):
        cmd.Cmd.__init__(self)
    
    ...
    do_serve(self, **kwargs):
        server = MyServer(self.loop, **kwargs)
        instance = server.start()
    
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    menu = MyMenu(loop)
    menu.cmdloop()

Calling loop.run_forever() at any point breaks the menu functionality.  Any help would be greatly appreciated.  




More information about the Python-list mailing list