Skip to content

Instantly share code, notes, and snippets.

@synapticarbors
Last active August 6, 2020 02:40
Show Gist options
  • Save synapticarbors/38e7fda1d5c23fc80b8562d41b7b672d to your computer and use it in GitHub Desktop.
Save synapticarbors/38e7fda1d5c23fc80b8562d41b7b672d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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