2 回答
TA贡献1877条经验 获得超1个赞
为什么不尝试一下线程概念呢?
import pysftp as sftp
from threading import Thread
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
handle = sftp.Connection('10.0.2.10',username='kali', password='root', cnopts=cnopts)
def exec(cmd):
#handle will be taken from the prev declaration
handle.execute(cmd)
list_cmds = ['/tmp/doThis', '/tmp/doThat']
for cmd in list_cmds:
Thread(target=exec, args=[cmd]).start()
TA贡献1744条经验 获得超4个赞
execute
一种选择是使用该模块为每个语句启动一个新进程multiprocessing
(。您使用Process
构造函数创建一个进程,为其提供要执行的目标函数和任何参数,并告诉它使用该start
方法启动其函数。如果您想等待进程完成,请使用join
该函数的方法。您的代码可能如下所示(确保您的语句包含在一个if __name__ == '__main__':
块中):
import pysftp as sftp
from multiprocessing import Process
if __name__ == '__main__':
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
handle = sftp.Connection(
'10.0.2.10',
username='kali',
password='root',
cnopts=cnopts
)
# Create processes
p1 = Process(target=handle.execute, args=('/tmp/doThis',))
p2 = Process(target=handle.execute, args=('/tmp/doThat',))
# Tell processes to start
p1.start()
p2.start()
# If you want to, then wait for both processes to finish
p1.join()
p2.join()
添加回答
举报