Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Created December 4, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimundi/b6736e2eb69de71ddab588e3093597e0 to your computer and use it in GitHub Desktop.
Save Kimundi/b6736e2eb69de71ddab588e3093597e0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import threading
import multiprocessing
def fork_join(jobs, results, runner, cores=None):
class SimpleCtrl:
def __init__(self):
self.aborted=False
def thread_main(jobs, results, runner, ctrl):
while jobs:
job = jobs.pop(0)
result = runner(job)
if ctrl.aborted:
break
results.append(result)
if not cores:
cores = multiprocessing.cpu_count()
ctrl = SimpleCtrl()
try:
threads = []
for i in range(0, cores):
thread = threading.Thread(target=thread_main,args=(jobs,results,runner,ctrl))
thread.daemon = True
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
except BaseException as e:
ctrl.aborted=True
raise e
# ------------------------------------------
import subprocess
import random
jobs = []
for i in range(0, 100):
jobs.append((random.random()*10.0 + 5.0, i))
results = []
try:
def thread(job):
time, i = job
subprocess.run(f"sleep {time}; echo thread {i:03} slept {time:.2f}", shell=True)
return i
fork_join(jobs, results, thread)
except KeyboardInterrupt as e:
print("abort")
print("---")
last = [0, 0]
for i in sorted(results):
if i == last[1] + 1:
last[1] += 1
else:
print(last)
last = [i, i]
print(last)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment