1
0
Fork 0
tinygrab/extra/helpers.py

25 lines
923 B
Python
Raw Normal View History

2023-03-04 00:07:46 -07:00
import time
class Timing(object):
2023-03-10 11:48:10 -07:00
def __init__(self, prefix="", on_exit=None, enabled=True): self.prefix, self.on_exit, self.enabled = prefix, on_exit, enabled
def __enter__(self): self.st = time.perf_counter_ns()
def __exit__(self, exc_type, exc_val, exc_tb):
self.et = time.perf_counter_ns() - self.st
if self.enabled: print(f"{self.prefix}{self.et*1e-6:.2f} ms"+(self.on_exit(self.et) if self.on_exit else ""))
2023-05-06 12:56:09 -06:00
def enable_early_exec():
import subprocess, multiprocessing
2023-05-06 13:18:54 -06:00
qin: multiprocessing.Queue = multiprocessing.Queue()
qout: multiprocessing.Queue = multiprocessing.Queue()
2023-05-06 12:56:09 -06:00
def _early_exec_process(qin, qout):
while 1:
path, inp = qin.get()
qout.put(subprocess.check_output(path, input=inp))
p = multiprocessing.Process(target=_early_exec_process, args=(qin, qout))
p.daemon = True
p.start()
def early_exec(x):
qin.put(x)
return qout.get()
return early_exec