简体   繁体   中英

python multithreading, how can i run multiple operations at once

am trying to sftp to remote machine

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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