Created
October 27, 2018 08:24
-
-
Save aliva/9af3a24357f1ec77ee677bf38d7cc60b to your computer and use it in GitHub Desktop.
A sample code for python ParallelQueue
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 queue | |
import threading | |
import time | |
from random import randint | |
class ParallelQueue: | |
def _worker(self, i): | |
print("Starting worker: ", i) | |
while True: | |
item = self.queue.get() | |
if item is None: | |
print("Stopping worker: ", i) | |
break | |
try: | |
func(item) | |
except Exception as e: | |
print(e) | |
self.queue.task_done() | |
def _start_workers(self): | |
self.threads = [] | |
for i in range(self.worker_count): | |
t = threading.Thread(target=self._worker, args=[i, ]) | |
t.start() | |
self.threads.append(t) | |
def run(self, worker_count, func, iterator): | |
self.queue = queue.Queue() | |
self.worker_count = worker_count | |
self._start_workers() | |
for item in iterator: | |
self.queue.put(item) | |
# block until all tasks are done | |
self.queue.join() | |
# stop workers | |
for i in range(worker_count): | |
self.queue.put(None) | |
for t in self.threads: | |
t.join() | |
def func(i): | |
print('task {i} started'.format(i=i)) | |
time.sleep(randint(1, 5)) | |
print('task {i} ended'.format(i=i)) | |
ParallelQueue().run(3, func, range(10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment