Jed*_*Jed 3 python ftp multithreading asynchronous pool
我需要从单个服务器中的单个文件夹下载许多文件,因此我正在寻找一种更快的方法。经过一番阅读后,似乎多线程或异步方法都可以工作,但我似乎无法让这两种方法都工作。
我正在使用的异步方法如下。这可行,即没有错误,但它一次只下载一个文件,因此不会提高速度。有没有办法修改它以便提高速度?
async def get_file(self):
async with aioftp.ClientSession(self.host, self.port, self.login, self.password) as client:
async for path, info in client.list(recursive=True):
if info["type"] == "file":
await client.download(path, destination=self.dest_dir,write_into=True, block_size=self.block_size)
def async_update(self):
loop = asyncio.get_event_loop()
loop.run_until_complete(self.get_file())
loop.close()
Run Code Online (Sandbox Code Playgroud)
然后我尝试在多处理中使用简单的 Pool() 函数,如下所示:
def simple_fetch(self,file)
file = open(self.dest_dir+filename, 'wb')
ftp.retrbinary('RETR ' + filename, file.write, 8192*(2^3)) #, 8192)
file.close()
def multi_fetch(self):
pool = Pool()
pool.map(self.simple_fetch,self.update_files)
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud)
但这会失败并出现错误。我一回到服务器就会更新该错误。
| 归档时间: |
|
| 查看次数: |
3898 次 |
| 最近记录: |