Last active
August 6, 2020 02:40
-
-
Save synapticarbors/38e7fda1d5c23fc80b8562d41b7b672d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
import numpy as np | |
class Runner: | |
def __init__(self): | |
self.is_running = True | |
self.threads = [] | |
for k in range(10): | |
t = threading.Thread(target=self.do_work, name=str(k), args=(5.0, )) | |
self.threads.append(t) | |
t.start() | |
# time.sleep(0.5) | |
def do_work(self, delta_t): | |
last = time.perf_counter() | |
print('starting thread: ', threading.current_thread().name) | |
while self.is_running: | |
if (time.perf_counter() - last) > delta_t: | |
last = time.perf_counter() | |
data = np.random.random((1000, 100)) | |
_ = np.linalg.svd(data, full_matrices=False) | |
else: | |
diff = delta_t - (time.perf_counter() - last) | |
if diff > 0: | |
time.sleep(diff) | |
def stop(self): | |
self.is_running = False | |
print('stopping threads') | |
for t in self.threads: | |
t.join() | |
print('threads stopped') | |
def run(): | |
start_time = time.perf_counter() | |
r = Runner() | |
while time.perf_counter() - start_time < 100: | |
time.sleep(1.0) | |
r.stop() | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment