簡體   English   中英

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

日志文件已關閉,請檢查線程是否正在使用日志文件,其他線程是否正在關閉日志文件,而其他正在使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM