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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With