Created
February 1, 2020 11:30
-
-
Save jaycosaur/6d72915cde8ed3f0fbb0aebb314aca30 to your computer and use it in GitHub Desktop.
QueuePipe joins forwards messages from one theading queue to another, can be stopped by invoking the stop() method.
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 queue import Queue | |
from threading import Thread | |
class KillSignal: | |
pass | |
class QueuePipe: | |
def __init__(self, queue_in: Queue, queue_out: Queue) -> None: | |
self._queue_in = queue_in | |
self._queue_out = queue_out | |
self.thread = Thread(target=self._pipe, args=(self._queue_in, self._queue_out,)) | |
self.thread.start() | |
def _pipe(self, queue_in: Queue, queue_out: Queue) -> None: | |
while True: | |
mes_in = queue_in.get() | |
queue_out.put(mes_in) | |
if isinstance(mes_in, KillSignal): | |
return | |
def stop(self) -> None: | |
self._queue_in.put(KillSignal()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment