Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: A Future or coroutine is required

I try make auto-reconnecting ssh client on asyncssh. (SshConnectManager must stay in background and make ssh sessions when need)

class SshConnectManager(object):
def __init__(self, host, username, password, port=22):
    self._host = host
    self._username = username
    self._password = password
    self._port = port

    self.conn = None
    asyncio.async(self.start_connection)

@asyncio.coroutine
def start_connection(self):
    try:
        Client = self._create_ssh_client()
        self.conn, _ = yield from asyncssh.create_connection(Client,
                                                        self._host, port=self._port,
                                                        username=self._username,
                                                        password=self._password)
    except Exception as e:
        print("Connection error! {}".format(e))
        asyncio.async(self.start_connection())

def _create_ssh_client(self):
    class MySSHClient(asyncssh.SSHClient):
        parent = self
        def connection_lost(self, exc):
            self.parent._handle_connection_lost(exc)
    return MySSHClient

def _handle_connection_lost(self, exc):
    print('Connection lost on {}'.format(self.host))
    print(exc)
    asyncio.async(self.start_connection)


ssh1 = SshConnectManager(settings.host, settings.username, settings.password, settings.port)

asyncio.get_event_loop().run_until_complete(...)

Please do not look at _create_ssh_client or other "haks"

Problem is:

$ python3 main.py 
Traceback (most recent call last):
  File "main.py", line 75, in <module>
    ssh1 = SshConnectManager(settings.host, settings.username, settings.password, settings.port)
  File "main.py", line 22, in __init__
    asyncio.async(self.start_connection)
  File "/usr/lib/python3.4/asyncio/tasks.py", line 565, in async
    raise TypeError('A Future or coroutine is required')
TypeError: A Future or coroutine is required

But self.start_connection is corutine! Or not? Or what is another way start async task from sync code?

like image 503
kolko Avatar asked May 26 '15 05:05

kolko


1 Answers

Thanks @dano and @boardrider for help in comments. Bug was what @asyncio.coroutine return function what need to call to get generator object. I forget to do this.

Fixed version:

class SshConnectManager(object):
def __init__(self, host, username, password, port=22):
    self._host = host
    self._username = username
    self._password = password
    self._port = port

    self.conn = None
    # FIX HERE
    asyncio.async(self.start_connection())

@asyncio.coroutine
def start_connection(self):
    try:
        Client = self._create_ssh_client()
        self.conn, _ = yield from asyncssh.create_connection(Client,
                                                        self._host, port=self._port,
                                                        username=self._username,
                                                        password=self._password)
    except Exception as e:
        print("Connection error! {}".format(e))
        asyncio.async(self.start_connection())

def _create_ssh_client(self):
    class MySSHClient(asyncssh.SSHClient):
        parent = self
        def connection_lost(self, exc):
            self.parent._handle_connection_lost(exc)
    return MySSHClient

def _handle_connection_lost(self, exc):
    print('Connection lost on {}'.format(self.host))
    print(exc)
    # AND HERE
    asyncio.async(self.start_connection())


ssh1 = SshConnectManager(settings.host, settings.username, settings.password, settings.port)

asyncio.get_event_loop().run_until_complete(...)

P.S. But i don't undestood why coroutine decorator can't return called decorator. (This make me confuse, i confuse this with twisted callbacks).

And i found how to remember this, have simple case, if start_connection can get arguments:

@asyncio.coroutine
def start_connection(self, some_arg):
    pass

so, i can simple write:

asyncio.async(self.start_connection(some_val))

and not need to make additional attributes in asyncio.async function

like image 163
kolko Avatar answered Oct 07 '22 17:10

kolko