Paramiko - Running commands in “background”

萝らか妹 提交于 2019-12-11 13:05:41

问题


I've successfully implemented Paramiko using exec_command, however, the command I'm running on the remote machine(s) can sometimes take several minutes to complete.

During this time my Python script has to wait for the remote command to complete and receive stdout.

My goal is to let the remote machine "run in the background", and allow the local Python script to continue once it sends the command via exec_command.

I'm not concerned with stdout at this point, I'm just interested in bypassing waiting for stdout to return so the script can continue on while the command runs on the remote machine.

Any suggestions?

Current script:

def function():
    ssh_object = paramiko.SSHClient()
    ssh_object.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_object.connect(address, port=22, username='un', password='pw')
    command = 'command to run'

try:
    stdin, stdout, stderr = ssh_object.exec_command(command)
    stdout.readlines()
except:
    do something else

Thank you!


回答1:


Use a separate thread to run the command. Usually threads should be cleaned up with the join command (the exception are daemon threads that you expect to run until your program exits). Exactly how you do that depends on the other stuff your program is running. But an example is:

import threading

def ssh_exec_thread(ssh_object, command):
    stdin, stdout, stderr = ssh_object.exec_command(command)
    stdout.readlines()

def function():
    ssh_object = paramiko.SSHClient()
    ssh_object.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_object.connect(address, port=22, username='un', password='pw')
    command = 'command to run'

    thread = threading.Thread(target=ssh_exec_thread, args=(ssh_object, command)
    thread.start()
    ...do something else...
    thread.join()

You can make this fancier by passing a Queue to ssh_exec_command and put the result on the queue for processing by your program later.



来源:https://stackoverflow.com/questions/29396066/paramiko-running-commands-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!