使用ftplib进行多线程上传
我正在尝试进行多线程上传,但出现错误。 我猜想也许不可能在 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
如果我尝试一张一张上传,一切正常
I'm trying to do multithread uploads, but get errors.
I guessed that maybe it's impossible to use multithreads with ftplib?
Here comes my code:
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()
Get bunch of ftplib errors like
raise error_reply, resp
error_reply: 200 Type set to I
If I try to upload one by one, everything works fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将连接代码放入线程内?
换句话说,让每个线程使用 FTP.host() 和 FTP.login() 进行自己单独的连接。服务器可能不喜欢在单个连接上同时进行多个上传,因为它可能一次解析一个命令,并且无法处理第二个上传或“STOR”命令。但是,如果您可以从同一 IP 地址进行多个连接,您将拥有单独的会话来发出“STOR”命令。
这是一个例子:
看看效果是否更好。
PS 不确定我的所有选项卡是否对齐。
Have you tried to put the connection code inside the thread?
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.
Here's an example:
See if that behaves better.
P.S. Not sure if all my tabs are aligned.
我最终使用信号量将 FTP 连接的使用一次限制为一个线程。共享连接比为每个线程创建连接更快。在你的情况下,它看起来像:
I ended up using the Semaphore to limit the usage of FTP connection only to one thread at a time. It is faster to share connection rather than creating connection for each thread. In your case it would look like: