Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Event when one remote command has finished #296

Answered by pkittenis
f380cedric asked this question in Q&A
Discussion options

Hi pkittenis,

Thx for your work!

I have to run commands that do not terminate on multiple remotes.
I am interested in getting notified when any remote command finished (e.g. the command encountered an unexpected error and exited, the process crashed, etc.).

While browsing the documentation,
I could not find any other way than polling around this snipped:

try:
 client.join(output, timeout=5)
except Timeout as ex:
 # Some commands timed out
 finished_output = ex.args[2]
 unfinished_output = ex.args[3]
else:
 # No timeout, all commands finished within five seconds
 finished_output = output
 unfinished_output = None

and checking if finished_output is populated.
Do you know any other way?

Regards

You must be logged in to vote

Hi,

Thanks for the interest. Short answer is no. Callback support is tracked by #275.

It could be done client side by spawning callback greenlets on top of SSHClient.wait_finished. Eg, something like:

from gevent import spawn
def my_cb(host_out):
 try:
 host_out.client.wait_finished()
 except Exception as ex:
 exc = ex
 else:
 exc = None
 print(f"Command on host {host_out.host} exited with error code {host_out.exit_code}, exception {exc}")
output = client.run_command(<..>)
callbacks = [spawn(my_cb, host_out) for host_out in output]

Can similarly do something other than print in the callback.

wait_finished will block the current greenlet. Anything afte...

Replies: 1 comment

Comment options

Hi,

Thanks for the interest. Short answer is no. Callback support is tracked by #275.

It could be done client side by spawning callback greenlets on top of SSHClient.wait_finished. Eg, something like:

from gevent import spawn
def my_cb(host_out):
 try:
 host_out.client.wait_finished()
 except Exception as ex:
 exc = ex
 else:
 exc = None
 print(f"Command on host {host_out.host} exited with error code {host_out.exit_code}, exception {exc}")
output = client.run_command(<..>)
callbacks = [spawn(my_cb, host_out) for host_out in output]

Can similarly do something other than print in the callback.

wait_finished will block the current greenlet. Anything after it will only be run if the command terminates for whatever reason. wait_finished needs to be run in a new greenlet to run in background.

You must be logged in to vote
0 replies
Answer selected by pkittenis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /