add callback when not listening

19 views
Skip to first unread message

Klutz ExNW

unread,
Jun 17, 2019, 4:41:33 PM6/17/19
to asyncssh-users
Hey,

how can i add callback to the session so everytime the device sends data to me over the session it will be called,
but if i am manually using readuntil it won't trigger.

Regards,
Syniex

Ron Frederick

unread,
Jun 17, 2019, 11:31:13 PM6/17/19
to Klutz ExNW, asyncssh-users
On Jun 17, 2019, at 1:41 PM, Klutz ExNW <klut...@gmail.com> wrote:
how can i add callback to the session so everytime the device sends data to me over the session it will be called,
but if i am manually using readuntil it won't trigger.

AsyncSSH supports either a callback-based API or a stream-based API (where you await on things like readuntil), but it’s not really designed to let you mix the two. That gets very messy for a couple of reasons. First, there’d be a race condition between data arriving and the application performing a read. As soon as one read completed, there’d be a chance that more data could arrive before read is called again, triggering the callback when the application really wanted to do both reads in a blocking matter. Second, a caller may not read all of the data which arrives, leaving some data buffered. If there’s data buffered, should the callback be called with the remaining data, or should it be left buffered for the next read call? What if more data arrives while there’s data buffered, without a read outstanding? Should the callback be called on both the buffered data and the new data? I wouldn’t think you’d want to deliver data out of order here, so you can’t just trigger the callback on the new data.

If there are specific times in your application where you want arriving data to trigger a callback, your best bet is to explicitly schedule a task to run in the background reading data and calling your callback during that time. Later, if you want AsyncSSH to stop calling the callback and let you read the data in the foreground, you can cancel that task. That way, you’re always using the stream-based API, but you can decide when to read using an await vs. reading in the background and triggering a callback. There’s still a bit of a race there, unfortunately, but if you know no more data will be arriving when you switch between the two states, you could probably make something like that work.
-- 
Ron Frederick
ro...@timeheart.net



Klutz ExNW

unread,
Jun 24, 2019, 12:16:20 PM6/24/19
to asyncssh-users
Can you provide me example using the callback,
right now this is out i connecting to the device


async def _establish_connection(self) -> Tuple[bool, str]:
f'Host "{self._host}": Establishing connection to port {self._port}'
)
fut = asyncssh.connect(**self._params)
try:
self._conn = await asyncio.wait_for(fut, self._timeout)
except asyncssh.DisconnectError as err:
# raise DisconnectError(self._host, err.code, )
return False, err.reason
except asyncio.TimeoutError:
reason = f"host didn't responsed for {self._timeout}s"
return False, reason
# raise TimeoutError(self._host, reason
self._stdin, self._stdout, self._stderr = await self._conn.open_session(
term_type="Dumb"
)

בתאריך יום שלישי, 18 ביוני 2019 בשעה 06:31:13 UTC+3, מאת Ron Frederick:

Ron Frederick

unread,
Jun 25, 2019, 12:34:09 AM6/25/19
to Klutz ExNW, asyncssh-users
You can find an example of using callbacks at https://asyncssh.readthedocs.io/en/latest/#callback-example.

Basically, you need to define your own subclass of SSHClientSession and implement the data_received() method on that, along with possibly other methods such as connection_made(), eof_received(), and connection_lost(). See https://asyncssh.readthedocs.io/en/latest/api.html#sshclientsession for a complete list of callbacks.Note that these are regular methods (not async), and they cannot await on things. They should update the state of the session based on the information they receive and immediately return. If they need to wait for something before taking an action, they should wait for that event to occur and perform the action in the callback where it does.
Reply all
Reply to author
Forward
0 new messages