Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

10 of 11
perfection

As I understood:

  • something is needed to be loaded
  • it is needed to be loaded often, because file with code which uses this something is edited often
  • you don't want to wait until it will be loaded every time

Maybe such solution will be okay for you.

You can write script loader file in such way (tested on Python 3.8):

import importlib.util, traceback, sys, gc, pickle
# Example data
something = pickle.loads(pickle.dumps([123]))
modules_before = list(sys.modules.keys())
while True:
 # Change according the file name
 mod_name, mod_path = '__main__', 'some_file.py'
 spec = importlib.util.spec_from_file_location(mod_name, mod_path)
 mod = importlib.util.module_from_spec(spec)
 # Change to needed global name in the target module
 mod.something = something
 sys.modules[mod_name] = mod
 try:
 spec.loader.exec_module(mod)
 except:
 traceback.print_exc()
 del mod, spec
 modules_after = list(sys.modules.keys())
 for k in modules_after:
 if k not in modules_before:
 del sys.modules[k]
 gc.collect()
 # Press enter to restart
 print('Press enter to re-run, CTRL-C to exit')
 sys.stdin.readline()

Example of module:

# Change 1 to some different number when first script is running and press enter
something[0] += 1 
print(something)

Should work. And should reduce the reload time of pickle close to zero 🌝

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /