From 49c83f0c693890f1e2df33919606362890bf726d Mon Sep 17 00:00:00 2001 From: ngharo Date: Thu, 28 Jun 2018 23:35:32 -0500 Subject: WIP --- lib/__init__.py | 0 lib/worker.py | 23 +++++++++++++++++++++++ spawn.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 lib/__init__.py create mode 100644 lib/worker.py create mode 100644 spawn.py diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/worker.py b/lib/worker.py new file mode 100644 index 0000000..a3ee435 --- /dev/null +++ b/lib/worker.py @@ -0,0 +1,23 @@ +import threading +import time + +def name(): + return threading.currentThread().getName() + +def worker(q): + while True: + part = q.get() + + if part is None: + break + + do_work(part) + time.sleep(0.05) + q.task_done() + +def do_work(part: dict): + if part['type'] == "print": + action_print(part['payload']) + +def action_print(text: str): + print('{0} - {1}'.format(name(), text)) diff --git a/spawn.py b/spawn.py new file mode 100644 index 0000000..decea85 --- /dev/null +++ b/spawn.py @@ -0,0 +1,29 @@ +import sys +import queue +import threading +from lib import worker + +num_worker_threads = 4 + +def input(): + return sys.stdin + +q = queue.Queue() +threads = [] +for i in range(num_worker_threads): + thread_name = 'bob-' + str(i) + t = threading.Thread(target=worker.worker, args=(q,), name=thread_name) + t.start() + threads.append(t) + +for item in input(): + q.put(item) + +# block until all tasks are done +q.join() + +# stop workers +for i in range(num_worker_threads): + q.put(None) +for t in threads: + t.join() -- cgit v1.2.3