作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当使用SFTP上传100个100字节的文件时,这里需要17秒(连接建立后,我什至没有计算初始连接时间)。这意味着仅传输 10 KB 需要 17 秒,即 0.59 KB/秒!
我知道向 open
、write
、close
等发送 SSH 命令可能会产生很大的开销,但是,在使用 SFTP 发送许多小文件时,有没有办法加快处理速度?
或 paramiko
/pysftp
中的特殊模式将所有要执行的写入操作保存在内存缓冲区中(假设最后 2 秒的所有操作),以及然后在一个分组的 SSH/SFTP channel 中做所有事情?这样可以避免在每个操作之间等待 ping 时间。
注意:
import pysftp, time, os
with pysftp.Connection('1.2.3.4', username='root', password='') as sftp:
with sftp.cd('/tmp/'):
t0 = time.time()
for i in range(100):
print(i)
with sftp.open('test%i.txt' % i, 'wb') as f: # even worse in a+ append mode: it takes 25 seconds
f.write(os.urandom(100))
print(time.time() - t0)
最佳答案
使用以下方法(100 个异步任务),大约 0.5 秒即可完成,这是一个巨大的改进。
import asyncio, asyncssh # pip install asyncssh
async def main():
async with asyncssh.connect('1.2.3.4', username='root', password='') as conn:
async with conn.start_sftp_client() as sftp:
print('connected')
await asyncio.wait([sftp.put('files/test%i.txt' % i) for i in range(100)])
asyncio.run(main())
我将探索源代码,但我仍然不知道它是否将许多操作分组在几个 SSH 事务中,或者它是否只是并行运行命令。
关于python - 使用 SFTP 缓慢上传许多小文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65106405/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!