使用ftplib进行多线程上传

3 投票
2 回答
6535 浏览
提问于 2025-04-15 21:04

我正在尝试进行多线程上传,但遇到了错误。

我猜可能无法在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的错误,比如:

raise error_reply, resp
error_reply: 200 Type set to I

如果我尝试一个一个上传,一切都正常。

2 个回答

1

我最后使用了信号量来限制FTP连接的使用,每次只能让一个线程使用。这种方式比每个线程都创建一个新的连接要快得多。在你的情况下,代码看起来会是这样的:

from threading import Semaphore

ftp_semaphore = Semaphore(1)  # limiting connection to only one thread

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")
    with ftp_semaphore:     
        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()
5

你有没有试过把连接的代码放到线程里面呢?

换句话说,就是让每个线程自己单独连接FTP,比如用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()

看看这样运行得怎么样。

附注:我不太确定我的缩进是否对齐。

撰写回答