Skip to content

Commit 426bff5

Browse files
authored
Merge pull request #106 from dihm/mod_watcher_imp_fix
Module watcher imp removal
2 parents b0882cd + 189c3cf commit 426bff5

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

labscript_utils/modulewatcher.py

+31-16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
import site
1818
import sysconfig
1919

20-
# deal with removal of imp from python 3.12
21-
try:
22-
import imp
23-
except ImportError:
24-
import _imp as imp
25-
2620

2721
# Directories in which the standard library and installed packages may be located.
2822
# Modules in these locations will be whitelisted:
@@ -63,16 +57,8 @@ def mainloop(self):
6357
while True:
6458
time.sleep(1)
6559
with self.lock:
66-
# Acquire the import lock so that we don't unload modules whilst an
67-
# import is in progess:
68-
imp.acquire_lock()
69-
try:
70-
if self.check():
71-
self.unload()
72-
finally:
73-
# We're done mucking around with the cached modules, normal imports
74-
# in other threads may resume:
75-
imp.release_lock()
60+
if self.check():
61+
self.unload()
7662

7763
def check(self):
7864
unload_required = False
@@ -138,3 +124,32 @@ def unload(self):
138124
# code holds references to sys.meta_path, and to preserve order, since order
139125
# is relevant.
140126
sys.meta_path[:] = self.meta_whitelist
127+
128+
if __name__ == "__main__":
129+
130+
from pathlib import Path
131+
import time
132+
133+
dict1 = {'t': 5, 'val': 10}
134+
dict2 = {'t': 5, 'val': 11}
135+
136+
print('ModuleWatcher instatiated in debug mode')
137+
module_watcher = ModuleWatcher(debug=True)
138+
139+
# import a local module
140+
import labscript_utils.dict_diff
141+
print('imported labscript_utils.dict_diff')
142+
print(labscript_utils.dict_diff.dict_diff(dict1, dict2))
143+
print('used dict_diff function, waiting 2 seconds for module watcher to update')
144+
time.sleep(2)
145+
146+
# now pretend it has been updated
147+
ex_mod = Path('dict_diff.py')
148+
ex_mod.touch()
149+
print('dict_diff module touched, waiting 2 seconds for ModuleWatcher to notice')
150+
time.sleep(2)
151+
152+
print(labscript_utils.dict_diff.dict_diff(dict1, dict2))
153+
print('Used dict_diff again, waiting 2 seconds for ModuleWatcher to not do anything')
154+
time.sleep(2)
155+

0 commit comments

Comments
 (0)