使用ftplib进行多线程上传 [英] Using ftplib for multithread uploads

查看:303
本文介绍了使用ftplib进行多线程上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行多线程上传,但出现错误. 我猜想也许无法在ftplib中使用多线程?

I'm trying to do multithread uploads, but get errors. I guessed that maybe it's impossible to use multithreads with 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()

获取诸如

引发error_reply,分别 error_reply:200类型设置为I

raise error_reply, resp error_reply: 200 Type set to I

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

If I try to upload one by one, everything works fine

推荐答案

您是否尝试过将连接代码放入线程内?

Have you tried to put the connection code inside the thread?

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

In other words, make each thread do their own separate connection with FTP.host() and FTP.login(). The server may not like multiple uploads at the same time on a single connection, because it may be parsing commands one at a time and can't handle a second upload or "STOR" command. But if you can do multiple connections from the same IP address, you'll have separate session on which to issue the 'STOR' command.

这是一个例子:

    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()

看看效果是否更好.

P.S.不确定我所有的标签是否对齐.

P.S. Not sure if all my tabs are aligned.

这篇关于使用ftplib进行多线程上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆