python多线程,我怎么能一次运行多个操作

时间:2017-02-10 08:38:07

标签: python multithreading python-2.7 python-3.x threadpoolexecutor

我正试图sftp到远程机器

使用我的功能

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()

我希望将文件并行复制到多个服务器

如果尝试过这个

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()

这给了我错误

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

1 个答案:

答案 0 :(得分:0)

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

关闭日志文件