Created
February 19, 2019 12:09
-
-
Save agmangas/d7753ed2ffe32160ba22d7f729296f64 to your computer and use it in GitHub Desktop.
Minimal multiprocessing PoC
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 | |
import queue | |
import random | |
import time | |
def process_loop(input_queue, output_queue): | |
print("Initializing process loop") | |
while True: | |
input_item = input_queue.get() | |
print("Processing item: {}".format(input_item)) | |
time.sleep(random.random() * 2.0) | |
output_queue.put({'result': random.choice([True, False])}) | |
class CapturingThread(object): | |
def __init__(self): | |
self._input_queue = multiprocessing.Queue(1) | |
self._output_queue = multiprocessing.Queue() | |
self._proc = multiprocessing.Process( | |
target=process_loop, | |
args=(self._input_queue, self._output_queue)) | |
def run(self): | |
self._proc.start() | |
while True: | |
frame = [random.random() for _ in range(5)] | |
print("Frame: {}".format(frame)) | |
try: | |
self._input_queue.put_nowait({'frame': frame}) | |
except queue.Full: | |
pass | |
while not self._output_queue.empty(): | |
result = self._output_queue.get_nowait() | |
print("Result from background process: {}".format(result)) | |
time.sleep(0.2) | |
if __name__ == '__main__': | |
cap_thr = CapturingThread() | |
cap_thr.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment