Python AsynchSSH SFTP示例似乎是按顺序而不是异步下载文件
我从网站上复制了示例代码,并对其稍作修改,以满足我的需求 我能够连接到SFTP站点并从“/Exports”文件夹下载文件,但似乎文件是一个接一个地下载的,而不是一次下载多个文件 我的代码:Python AsynchSSH SFTP示例似乎是按顺序而不是异步下载文件,python,asynchronous,sftp,Python,Asynchronous,Sftp,我从网站上复制了示例代码,并对其稍作修改,以满足我的需求 我能够连接到SFTP站点并从“/Exports”文件夹下载文件,但似乎文件是一个接一个地下载的,而不是一次下载多个文件 我的代码: import asyncio import asyncssh import sys async def run_client(): async with asyncssh.connect(host=host, username=username, password=password, port=p
import asyncio
import asyncssh
import sys
async def run_client():
async with asyncssh.connect(host=host, username=username, password=password, port=port_no, known_hosts=None) as conn:
async with conn.start_sftp_client() as sftp:
await sftp.get(
remotepaths='/Exports',
localpath=r'Path on my local machine',
preserve=True,
recurse=True,
max_requests=128
)
try:
asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
sys.exit('SFTP operation failed: ' + str(exc))
我需要下载9000个文件,大部分是1KB。我肯定能看到文件一个接一个的下载。知道问题是什么吗?这里有一个异步下载文件的解决方案
导入异步IO
导入异步SSH
异步def下载_文件(sftp,文件:str,localdir:str):
等待sftp.get(文件,localpath=f“{localdir}/{file}”)
异步def run_client():
与asynchssh.connect异步(
“主机”,username=“username”,password=“password”
)康涅狄格州:
与连接异步启动\u sftp\u客户端()作为sftp:
files=wait sftp.glob(“/Exports/*”)
tasks=(下载_文件(sftp,file,localdir=“/”)用于文件中的文件)
等待asyncio.gather(*任务)
asyncio.run(run\u client())
我不认为异步=并行。我相信代码做了它应该做的。如果需要并行下载,则需要运行多个并行操作。这是一个不错的解决方案,但如何限制异步请求的数量?最好每n个请求睡眠x秒。收集50个睡眠时间1秒。再聚集50人等等。。