Enable use as a context manager

This commit is contained in:
Jake Howard 2018-12-08 15:32:29 +00:00
parent 3a455ac306
commit 964398722a
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -24,6 +24,12 @@ class Subscriber:
def has_data(self):
return socket_has_data(self.socket)
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
def listen(self):
while True:
yield from self.get_message()
@ -58,6 +64,13 @@ class Publisher:
target=self._accept_new_connections
)
def __enter__(self):
self.start()
return self
def __exit__(self, *args):
self.close()
def start(self):
self.accepting_new_connections.set()
self.new_connections_thread.start()