我有一个进程,每隔5分钟使用netcdf4.Dataset(fn,mode = a)
来增加NetCDF文件fn
的大小。同时,我使用xarray.Dataset
创建了一个bokeh服务器可视化该NetCDF文件的对象(因为这样非常方便)。
问题在于,如果我的bokeh服务器正在使用fn
文件,则尝试向其添加新数据时NetCDF更新进程将失败。
ds = xarray.open_dataset(fn)
如果我使用选项
autoclose
ds = xarray.open_dataset(fn, autoclose=True)
当bokeh服务器应用程序中的ds
处于“打开”状态时,使用其他进程更新fn
是有效的,但从fn
获取时间片的bokeh图的更新速度非常缓慢。
我的问题是:在使用xarray.Dataset
时,是否有另一种释放NetCDF文件锁定的方法?
如果仅在重新加载整个bokeh服务器应用程序后一致地更新xarray.Dataset的形状,则我不会介意。
谢谢!
这是一个最小工作示例:
将以下内容放入文件中并运行:
import time
from datetime import datetime
import numpy as np
import netCDF4
fn = 'my_growing_file.nc'
with netCDF4.Dataset(fn, 'w') as nc_fh:
# create dimensions
nc_fh.createDimension('x', 90)
nc_fh.createDimension('y', 90)
nc_fh.createDimension('time', None)
# create variables
nc_fh.createVariable('x', 'f8', ('x'))
nc_fh.createVariable('y', 'f8', ('y'))
nc_fh.createVariable('time', 'f8', ('time'))
nc_fh.createVariable('rainfall_amount',
'i2',
('time', 'y', 'x'),
zlib=False,
complevel=0,
fill_value=-9999,
chunksizes=(1, 90, 90))
nc_fh['rainfall_amount'].scale_factor = 0.1
nc_fh['rainfall_amount'].add_offset = 0
nc_fh.set_auto_maskandscale(True)
# variable attributes
nc_fh['time'].long_name = 'Time'
nc_fh['time'].standard_name = 'time'
nc_fh['time'].units = 'hours since 2000-01-01 00:50:00.0'
nc_fh['time'].calendar = 'standard'
for i in range(1000):
with netCDF4.Dataset(fn, 'a') as nc_fh:
current_length = len(nc_fh['time'])
print('Appending to NetCDF file {}'.format(fn))
print(' length of time vector: {}'.format(current_length))
if current_length > 0:
last_time_stamp = netCDF4.num2date(
nc_fh['time'][-1],
units=nc_fh['time'].units,
calendar=nc_fh['time'].calendar)
print(' last time stamp in NetCDF: {}'.format(str(last_time_stamp)))
else:
last_time_stamp = '1900-01-01'
print(' empty file, starting from scratch')
nc_fh['time'][i] = netCDF4.date2num(
datetime.utcnow(),
units=nc_fh['time'].units,
calendar=nc_fh['time'].calendar)
nc_fh['rainfall_amount'][i, :, :] = np.random.rand(90, 90)
print('Sleeping...\n')
time.sleep(3)
然后,前往IPython并通过以下方式打开增长的文件:
ds = xr.open_dataset('my_growing_file.nc')
这将导致向NetCDF附加的进程失败,并输出以下内容:
Appending to NetCDF file my_growing_file.nc
length of time vector: 0
empty file, starting from scratch
Sleeping...
Appending to NetCDF file my_growing_file.nc
length of time vector: 1
last time stamp in NetCDF: 2018-04-12 08:52:39.145999
Sleeping...
Appending to NetCDF file my_growing_file.nc
length of time vector: 2
last time stamp in NetCDF: 2018-04-12 08:52:42.159254
Sleeping...
Appending to NetCDF file my_growing_file.nc
length of time vector: 3
last time stamp in NetCDF: 2018-04-12 08:52:45.169516
Sleeping...
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-17-9950ca2e53a6> in <module>()
37
38 for i in range(1000):
---> 39 with netCDF4.Dataset(fn, 'a') as nc_fh:
40 current_length = len(nc_fh['time'])
41
netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.__init__()
netCDF4/_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()
IOError: [Errno -101] NetCDF: HDF error: 'my_growing_file.nc'
If using
ds = xr.open_dataset('my_growing_file.nc', autoclose=True)
没有错误,但是通过
xarray
访问时间当然会变慢,这正是我的问题所在,因为我的仪表盘可视化非常卡顿。我可以理解这可能不是xarray
的预期用途,如果需要,我将退回到netCDF4
提供的更低级别接口(希望它支持并发文件访问,至少支持读取),但我想保留xarray
的方便性。
ds.close()
。 - jhammannetcdf4
和h5py
进行了一些实验。使用netcdf4
时,我没有成功地让一个文件在两个进程中同时打开。对于h5py.File
,有一个标志swmr
(单写多读),它特别指示我可以在另一个进程保持文件打开以进行写入时进行读取。这样做是可行的,尽管与我的示例略有不同,因为增长的文件必须始终保持打开状态,不能在for循环中关闭并重新打开。我是否遗漏了什么,或者使用h5py
是唯一的方法? - cchwala