Skip to content

Instantly share code, notes, and snippets.

@thomasweng15
Last active November 21, 2022 21:10
Show Gist options
  • Save thomasweng15/e7fd4f0bfe032264a9214df66d161504 to your computer and use it in GitHub Desktop.
Save thomasweng15/e7fd4f0bfe032264a9214df66d161504 to your computer and use it in GitHub Desktop.
multiprocessing with a queue and pool
// 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