Enable use as a context manager
This commit is contained in:
parent
3a455ac306
commit
964398722a
1 changed files with 13 additions and 0 deletions
|
@ -24,6 +24,12 @@ class Subscriber:
|
||||||
def has_data(self):
|
def has_data(self):
|
||||||
return socket_has_data(self.socket)
|
return socket_has_data(self.socket)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def listen(self):
|
def listen(self):
|
||||||
while True:
|
while True:
|
||||||
yield from self.get_message()
|
yield from self.get_message()
|
||||||
|
@ -58,6 +64,13 @@ class Publisher:
|
||||||
target=self._accept_new_connections
|
target=self._accept_new_connections
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.start()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.accepting_new_connections.set()
|
self.accepting_new_connections.set()
|
||||||
self.new_connections_thread.start()
|
self.new_connections_thread.start()
|
||||||
|
|
Reference in a new issue