简体   繁体   English

python多线程,我如何一次运行多个操作

[英]python multithreading, how can i run multiple operations at once

am trying to sftp to remote machine 正在尝试通过sftp到远程计算机

with my function 用我的功能

def copyToServer(hostname, username, password, destPath, localPath):
    transport = paramiko.Transport((hostname, 22))
    transport.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(transport)
    sftp.put(localPath, destPath)
    sftp.close()
    transport.close()

and i want to copy file to multiple servers in parallel 我想将文件并行复制到多个服务器

if have tried this 如果尝试过这个

for i in range(xxx.xxx.xxx.111-xxx.xxx.xxx.210):
    hostname = i
    username = defaultLogin
    password = defaultPassword
    thread = threading.Thread(target=copyToServer, args=(hostname, username, password, destPath, localPath))
    thread.start()

this gave me error 这给了我错误

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "testi.py", line 56, in copyToServer
    log.write("%s   FAILED    - copying failed    directory at remote machine doesn't exist\r\n" % hostname)
ValueError: I/O operation on closed file

日志文件已关闭,请检查线程是否正在使用日志文件,其他线程是否正在关闭日志文件,而其他正在使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM