prototype
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import sys
|
||||
import importlib.util
|
||||
|
||||
|
||||
def load_module(module_name, file_path):
|
||||
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[module_name] = module
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from pathlib import Path
|
||||
from time import sleep
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from importing import load_module
|
||||
|
||||
|
||||
def load_files(folder):
|
||||
folder = Path(folder)
|
||||
fnames = folder.glob("*.py")
|
||||
fnames = sorted(fnames)
|
||||
|
||||
res = {}
|
||||
for fn in fnames:
|
||||
print(fn, fn.stat().st_mtime)
|
||||
name = fn.stem
|
||||
mod = load_module(name, fn)
|
||||
res[name] = mod.run
|
||||
return res
|
||||
|
||||
|
||||
folder = "scripts"
|
||||
mods = load_files(folder)
|
||||
|
||||
while True:
|
||||
with ThreadPoolExecutor() as ex:
|
||||
for f in mods.values():
|
||||
fut = ex.submit(f)
|
||||
print(fut)
|
||||
|
||||
sleep(1)
|
||||
|
||||
|
||||
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from time import sleep
|
||||
|
||||
def run():
|
||||
print("Test Sleep: start")
|
||||
sleep(1)
|
||||
print("Test Sleep: done")
|
||||
|
||||
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
N = 0
|
||||
|
||||
def run():
|
||||
global N
|
||||
print("Test Global Variable:", N)
|
||||
N += 1
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class Counter:
|
||||
|
||||
def __init__(self):
|
||||
self.n = 0
|
||||
|
||||
def inc(self):
|
||||
self.n += 1
|
||||
|
||||
def __str__(self):
|
||||
return str(self.n)
|
||||
|
||||
|
||||
counter = Counter()
|
||||
|
||||
|
||||
def run():
|
||||
print("Test Global Class:", counter)
|
||||
counter.inc()
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class Run:
|
||||
|
||||
def __init__(self):
|
||||
self.n = 0
|
||||
|
||||
def inc(self):
|
||||
self.n += 1
|
||||
|
||||
def __str__(self):
|
||||
return f"Test Callable Class: {self.n}"
|
||||
|
||||
def __call__(self):
|
||||
print(self)
|
||||
self.inc()
|
||||
|
||||
|
||||
run = Run()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user