Add support for an on_stop callback
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from threading import Thread, Event
|
||||
from typing import Callable
|
||||
|
||||
from .pipe_reader_thread import PipeReaderThread
|
||||
from .pipe_writer_thread import PipeWriterThread
|
||||
@ -15,6 +16,7 @@ class PipeIOServer(ABC, Thread):
|
||||
self._stop_event = Event()
|
||||
self._reader_thread, self._writer_thread = self._create_io_threads()
|
||||
self._io_threads = (self._reader_thread, self._writer_thread)
|
||||
self._on_stop = lambda: None
|
||||
|
||||
def _create_pipes(self, permissions):
|
||||
Pipe(self.in_pipe).recreate(permissions)
|
||||
@ -58,6 +60,14 @@ class PipeIOServer(ABC, Thread):
|
||||
thread.stop()
|
||||
Pipe(self.in_pipe).remove()
|
||||
Pipe(self.out_pipe).remove()
|
||||
self._on_stop()
|
||||
|
||||
def _set_on_stop(self, value):
|
||||
if not isinstance(value, Callable):
|
||||
raise ValueError("Supplied object is not callable!")
|
||||
self._on_stop = value
|
||||
|
||||
on_stop = property(fset=_set_on_stop)
|
||||
|
||||
def wait(self):
|
||||
self._stop_event.wait()
|
||||
|
Reference in New Issue
Block a user