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

python multithreading, how can i run multiple operations at once

我正在尝试通过 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

日志文件已关闭,请检查日志文件是否正在被线程使用以及其他线程在其他线程使用时关闭它