@@ -90,6 +90,13 @@ def tempdir_human():
90
90
return tempdir (now_filename ())
91
91
92
92
93
+ def temp_lock (path ):
94
+ from filelock import FileLock
95
+ import hashlib
96
+ lock = FileLock (f"{ tempdir ()} /.{ hashlib .md5 (path .encode ('utf8' )).hexdigest ()} .lock" )
97
+ return lock
98
+
99
+
93
100
def hanlp_home_default ():
94
101
"""Default data directory depending on the platform and environment variables"""
95
102
if windows ():
@@ -292,6 +299,7 @@ def get_resource(path: str, save_dir=hanlp_home(), extract=True, prefix=HANLP_UR
292
299
The real path to the resource.
293
300
294
301
"""
302
+ _path = path
295
303
path = hanlp .pretrained .ALL .get (path , path )
296
304
anchor : str = None
297
305
compressed = None
@@ -333,12 +341,17 @@ def get_resource(path: str, save_dir=hanlp_home(), extract=True, prefix=HANLP_UR
333
341
# realpath is where its path after exaction
334
342
if compressed :
335
343
realpath += compressed
336
- if not os .path .isfile (realpath ):
337
- path = download (url = path , save_path = realpath , verbose = verbose )
338
- else :
339
- path = realpath
344
+ with temp_lock (path ):
345
+ if not os .path .isfile (realpath ):
346
+ path = download (url = path , save_path = realpath , verbose = verbose )
347
+ else :
348
+ path = realpath
340
349
if extract and compressed :
341
- path = uncompress (path , verbose = verbose )
350
+ with temp_lock (path ):
351
+ if os .path .isfile (path ):
352
+ path = uncompress (path , verbose = verbose )
353
+ else : # other process must have already decompressed it and deleted it
354
+ return get_resource (_path , save_dir , extract , prefix , append_location , verbose )
342
355
if anchor :
343
356
path = path_join (path , anchor )
344
357
0 commit comments