Last active
November 21, 2022 21:10
-
-
Save thomasweng15/e7fd4f0bfe032264a9214df66d161504 to your computer and use it in GitHub Desktop.
multiprocessing with a queue and pool
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
// https://stackoverflow.com/questions/17241663/filling-a-queue-and-managing-multiprocessing-in-python | |
import time | |
import os | |
import multiprocessing as mp | |
from multiprocessing import Queue, Pool | |
the_queue = Queue() | |
def worker_main(queue): | |
print(f"{os.getpid()} working") | |
while True: | |
item = queue.get(True) | |
print(f"{os.getpid()} got {item}") | |
time.sleep(1) # simulate a "long" operation | |
the_pool = Pool(3, worker_main,(the_queue,)) | |
# don't forget the comma here ^ | |
for i in range(5): | |
the_queue.put("hello") | |
the_queue.put("world") | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment