读取使用Python Paramiko SFTPClient.open方法打开的文件很慢 [英] Reading file opened with Python Paramiko SFTPClient.open method is slow

查看:895
本文介绍了读取使用Python Paramiko SFTPClient.open方法打开的文件很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试远程读取netcdf文件.
我使用 paramiko 包读取文件,如下所示:

I am trying to remote read a netcdf file.
I used paramiko package to read my file, like this:

import paramiko
from netCDF4 import Dataset

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=’hostname’, username=’usrname’, password=’mypassword’)

sftp_client = client.open_sftp()
ncfile = sftp_client.open('mynetCDFfile')
b_ncfile = ncfile.read()    # ****

nc = Dataset('test.nc', memory=b_ncfile)

但是ncfile.read()的运行速度非常慢.
所以我的问题是:有没有其他方法可以远程读取netcdf文件,或者它有什么方法可以加快paramiko.sftp_file.SFTPFile.read()?

But the run speed of ncfile.read() is VERY SLOW.
So my question is: is there any alternative way to read a netcdf file remotely, or does it has any approach to speed up paramiko.sftp_file.SFTPFile.read()?

推荐答案

调用

Calling SFTPFile.prefetch should increase the read speed:

ncfile = sftp_client.open('mynetCDFfile')
ncfile.prefetch()
b_ncfile = ncfile.read()


另一个选项是使用bufsize参数启用读取缓冲="nofollow noreferrer"> SFTPClient.open :


Another option is enabling read buffering, using bufsize parameter of SFTPClient.open:

ncfile = sftp_client.open('mynetCDFfile', bufsize=32768)
b_ncfile = ncfile.read()

(32768SFTPFile.MAX_REQUEST_SIZE的值)

类似地用于写入/上传:
写入使用pysftp"open"打开的SFTP服务器上的文件方法很慢.

Similarly for writes/uploads:
Writing to a file on SFTP server opened using pysftp "open" method is slow.

另一种选择是显式指定要读取的数据量(它使 BufferedFile.read 采用更有效的代码路径):

Yet another option is to explicitly specify the amount of data to read (it makes BufferedFile.read take a more efficient code path):

ncfile = sftp_client.open('mynetCDFfile')
b_ncfile = ncfile.read(ncfile.stat().st_size)


强制性警告:请勿以这种方式使用AutoAddPolicy –您正在失去对 MITM攻击.有关正确的解决方案,请参见 Paramiko未知服务器" .


Obligatory warning: Do not use AutoAddPolicy this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

这篇关于读取使用Python Paramiko SFTPClient.open方法打开的文件很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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