Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用ftplib进行多线程上传_Python_Multithreading_Ftplib - Fatal编程技术网

Python 使用ftplib进行多线程上传

Python 使用ftplib进行多线程上传,python,multithreading,ftplib,Python,Multithreading,Ftplib,我正在尝试进行多线程上传,但出现了错误。 我猜可能无法在ftplib中使用多线程 下面是我的代码: class myThread (threading.Thread): def __init__(self, threadID, src, counter, image_name): self.threadID = threadID self.src = src self.counter = counter self.i

我正在尝试进行多线程上传,但出现了错误。 我猜可能无法在ftplib中使用多线程

下面是我的代码:

    class myThread (threading.Thread):
    def __init__(self, threadID, src, counter, image_name):
        self.threadID = threadID
        self.src = src
        self.counter = counter
        self.image_name = image_name
        threading.Thread.__init__(self)
    def run(self):
        uploadFile(self.src, self.image_name)

def uploadFile(src, image_name):
    f = open(src, "rb")            
    ftp.storbinary('STOR ' + image_name, f)
    f.close()

ftp = FTP('host')   # connect to host, default port
ftp.login()               # user anonymous, passwd anonymous@   
dirname = "/home/folder/"
i = 1   
threads = []

for image in os.listdir(dirname):
    if os.path.isfile(dirname + image):
        thread = myThread(i , dirname + image, i, image )   
        thread.start()
        threads.append( thread )        
        i += 1  

for t in threads:
    t.join()
获取大量ftplib错误,如

分别提出错误回复 错误\u回复:200类型设置为I


如果我尝试一个接一个地上传,一切正常

您是否尝试将连接代码放入线程中

换句话说,让每个线程分别与FTP.host和FTP.login进行连接。服务器可能不喜欢在单个连接上同时进行多个上载,因为它可能一次解析一个命令,并且无法处理第二个上载或STOR命令。但是,如果您可以从同一IP地址进行多个连接,您将有单独的会话来发出“STOR”命令

下面是一个例子:

    class myThread (threading.Thread):
        def __init__(self, threadID, src, counter, image_name):
             ###############
             #Add ftp connection here!
             self.ftp = FTP('host')   # connect to host, default port
             self.ftp.login()               # user anonymous, passwd anonymous@   
             ################
             self.threadID = threadID
             self.src = src
             self.counter = counter
             self.image_name = image_name
             threading.Thread.__init__(self)
        def run(self):
             uploadFile(self.src, self.image_name)

    def uploadFile(src, image_name):
          f = open(src, "rb")            
          self.ftp.storbinary('STOR ' + image_name, f)
          f.close()

     dirname = "/home/folder/"
     i = 1   
     threads = []

     for image in os.listdir(dirname):
          if os.path.isfile(dirname + image):
             thread = myThread(i , dirname + image, i, image )   
             thread.start()
             threads.append( thread )        
             i += 1  

      for t in threads:
          t.join()
看看这样是否更好


另外,不确定我的所有选项卡是否都对齐。

即使ftplib支持多线程,该功能如何工作?你的每个线程都试图上传同一个文件为什么是相同的?如果我只是在相同的“for”循环中调用函数而不使用线程,则工作正常。它从文件夹中传递所有文件,误读文件打开代码。不管怎样,我认为假设库不提供线程安全或并发ftp会话是非常安全的。总之,ftplib没有多线程支持,是吗?太糟糕了。即使连接良好,上传大量文件也需要很长时间,这是可行的,尽管这种方法比没有多线程的上传速度慢。所以我可能应该找另一个库,或者用一个线程上传古玩来解释为什么这会慢一些。有什么见解吗?