Created
December 27, 2021 07:36
-
-
Save methylDragon/db8020a315e2482cbb6a93e775975e9f to your computer and use it in GitHub Desktop.
Python3 Multiprocessing Skeleton with Data Sharing
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
| import multiprocessing as mp | |
| import time | |
| def producer(shared, end_event): | |
| count = 0 | |
| while not end_event.is_set(): | |
| time.sleep(1/30) | |
| count += 1 | |
| shared['count']['count'] = count | |
| print("Terminating producer") | |
| def consumer(shared, end_event): | |
| last_count = 0 | |
| while not end_event.is_set(): | |
| time.sleep(1/20) | |
| try: | |
| count = shared['count'].pop('count') | |
| except Exception as e: | |
| print(e) | |
| pass | |
| if count > last_count + 1: | |
| print(f"== MISSED FRAMES: {count - last_count - 1}") | |
| print(count) | |
| last_count = count | |
| print("Terminating consumer") | |
| frame_req.close() | |
| frame_resp.close() | |
| if __name__ == "__main__": | |
| end_event = mp.Event() | |
| manager = mp.Manager() | |
| shared = manager.dict() | |
| shared['count'] = manager.dict() | |
| frame_producer = mp.Process(target=producer, | |
| args=(shared, end_event)) | |
| frame_consumer = mp.Process(target=consumer, | |
| args=(shared, end_event)) | |
| try: | |
| frame_producer.start() | |
| frame_consumer.start() | |
| frame_producer.join() | |
| frame_consumer.join() | |
| except KeyboardInterrupt: | |
| print("CTRL+C") | |
| except Exception as e: | |
| print(e) | |
| finally: | |
| end_event.set() | |
| frame_producer.join() | |
| frame_consumer.join() | |
| manager.shutdown() |
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
| import multiprocessing as mp | |
| import time | |
| def producer(frame_req, frame_resp, end_event): | |
| count = 0 | |
| print("PRODUCER STARTED") | |
| while not end_event.is_set(): | |
| time.sleep(0.011) | |
| count += 1 | |
| if frame_req.poll(): | |
| frame_req.recv() | |
| frame_resp.send(count) | |
| print("Terminating producer") | |
| frame_req.close() | |
| frame_resp.close() | |
| def consumer(frame_req, frame_resp, end_event): | |
| print("CONSUMER STARTED") | |
| while not end_event.is_set(): | |
| time.sleep(0.009) | |
| frame_req.send(1) | |
| count = frame_resp.recv() | |
| print(count) | |
| print("Terminating consumer") | |
| frame_req.close() | |
| frame_resp.close() | |
| if __name__ == "__main__": | |
| # Use pipes to coordinate | |
| frame_req_in, frame_req_out = mp.Pipe() | |
| frame_resp_in, frame_resp_out = mp.Pipe() | |
| end_event = mp.Event() | |
| frame_producer = mp.Process(target=producer, | |
| args=(frame_req_out, frame_resp_in, end_event)) | |
| frame_consumer = mp.Process(target=consumer, | |
| args=(frame_req_in, frame_resp_out, end_event)) | |
| try: | |
| frame_producer.start() | |
| frame_consumer.start() | |
| frame_producer.join() | |
| frame_consumer.join() | |
| except KeyboardInterrupt: | |
| print("CTRL+C") | |
| except Exception as e: | |
| print(e) | |
| finally: | |
| end_event.set() | |
| frame_producer.join() | |
| frame_consumer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment