Rearrange cleanup for NFS stale handles

This commit is contained in:
Guilhem Lavaux 2018-01-05 22:17:26 +01:00
parent 531084dbc1
commit 945166c1f9

View File

@ -11,7 +11,7 @@ def smooth_particle_density(
velocities=None, velocities=None,
radius=1e6, radius=1e6,
boxsize=None, boxsize=None,
resolution=128,i resolution=128,
center=None, tmpprefix=None ): center=None, tmpprefix=None ):
"""Use adaptive smoothing to produce density and momentum fields. """Use adaptive smoothing to produce density and momentum fields.
The algorithm is originally described in [1]. The algorithm is originally described in [1].
@ -68,36 +68,38 @@ def smooth_particle_density(
raise ValueError("Need a boxsize") raise ValueError("Need a boxsize")
cx,cy,cz=center cx,cy,cz=center
with TemporaryDirectory(prefix=tmpprefix) as tmpdir: tmpdir = TemporaryDirectory(prefix=tmpprefix)
h5_file = os.path.join(tmpdir, 'particles.h5') h5_file = os.path.join(tmpdir.name, 'particles.h5')
with h5.File(h5_file, mode="w") as f: with h5.File(h5_file, mode="w") as f:
data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32) data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32)
data[:,:3] = position[:,:3] data[:,:3] = position[:,:3]
if velocities is not None: if velocities is not None:
data[:,3:6] = velocities[:,:3] data[:,3:6] = velocities[:,:3]
else: else:
data[:,3:6] = position[:,3:] data[:,3:6] = position[:,3:]
data[:,6] = 1 data[:,6] = 1
ret = \ ret = \
subprocess.run([ subprocess.run([
os.path.join(install_prefix,'bin','simple3DFilter'), os.path.join(install_prefix,'bin','simple3DFilter'),
h5_file, h5_file,
str(radius), str(radius),
str(boxsize), str(boxsize),
str(resolution), str(resolution),
str(cx), str(cy), str(cz) str(cx), str(cy), str(cz)
], cwd=tmpdir) ], cwd=tmpdir.name)
f0 = h5.File(os.path.join(tmpdir,'fields.h5'), mode="r") f0 = h5.File(os.path.join(tmpdir.name,'fields.h5'), mode="r")
def cleanup_f0(): def cleanup_f0():
f0.close() f0.close()
tmpdir.cleanup()
class Dict(dict): class Dict(dict):
pass pass
t = Dict(rho=f0['density'], p=[f0['p0'], f0['p1'], f0['p2']]) t = Dict(rho=f0['density'], p=[f0['p0'], f0['p1'], f0['p2']])
weakref.finalize(t, cleanup_f0) t._tmpdir_=tmpdir
return t weakref.finalize(t, cleanup_f0)
return t