Skip to content

Instantly share code, notes, and snippets.

@nipuntalukdar
Created July 5, 2024 07:36
Show Gist options
  • Select an option

  • Save nipuntalukdar/cca838999f1197658b547b286b30e92c to your computer and use it in GitHub Desktop.

Select an option

Save nipuntalukdar/cca838999f1197658b547b286b30e92c to your computer and use it in GitHub Desktop.
Example of using multiprocessing python module as work queue executor
from multiprocessing import Process, Queue
from queue import Empty
import os
from time import sleep
def fun(q, timeout):
while True:
try:
item = q.get(timeout=timeout)
print(item, os.getpid())
if item['what'] == 'exit':
exit(0)
sleep(1)
except Empty as emptyex:
print(emptyex)
if __name__ == '__main__':
q = Queue(maxsize=10)
processes = []
for i in range(4):
p = Process(target=fun, args=(q,4))
p.start()
processes.append(p)
for i in range(100):
q.put({'what' : 'do' , 'value' : i})
for i in range(4):
q.put({'what' : 'exit' , 'value' : i}, timeout=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment