Last active
February 13, 2019 09:57
-
-
Save stefpe/165e077ff2f89fe526992f4f44c07580 to your computer and use it in GitHub Desktop.
queue based threadings
This file contains hidden or 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 queue | |
| import threading | |
| NUM_THREADS = 10 | |
| def worker(channel): | |
| while True: | |
| value = channel.get() | |
| if value is False: | |
| break | |
| print('value = %d' % value) | |
| channel.task_done() | |
| channel = queue.Queue() | |
| print("start workers") | |
| workers = [] | |
| for val in range(NUM_THREADS): | |
| th = threading.Thread(target=worker, args=(channel,)) | |
| th.start() | |
| workers.append(th) | |
| for input in range(100): | |
| channel.put(input) | |
| channel.join() | |
| for i in range(NUM_THREADS): | |
| channel.put(False) | |
| for t in workers: t.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment