Created
July 5, 2024 07:36
-
-
Save nipuntalukdar/cca838999f1197658b547b286b30e92c to your computer and use it in GitHub Desktop.
Example of using multiprocessing python module as work queue executor
This file contains hidden or 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
| 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