Created
December 21, 2017 20:56
-
-
Save philippkeller/b839238ac125bbc982a56cf3ac484a5c to your computer and use it in GitHub Desktop.
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 multiprocessing | |
import queue | |
import time | |
def worker(input, output): | |
for line in iter(input.get, None): | |
output.put(len(line)) | |
if __name__ == '__main__': | |
num_processes = 3 | |
with open('text_big.txt') as f: | |
task_queue = multiprocessing.Queue(maxsize=10000) | |
done_queue = multiprocessing.Queue() | |
pool = multiprocessing.Pool(num_processes, worker,(task_queue,done_queue)) | |
for i, line in enumerate(f): | |
task_queue.put(line.strip()) | |
for _ in range(num_processes): | |
task_queue.put(None) | |
pool.close() | |
pool.join() | |
total = 0 | |
while not done_queue.empty(): | |
l = done_queue.get() | |
total += l | |
print(total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment