Skip to content

Instantly share code, notes, and snippets.

@ohadravid
Created April 19, 2025 10:12
Show Gist options
  • Select an option

  • Save ohadravid/25f0a4de72bd54042351541ef061ac49 to your computer and use it in GitHub Desktop.

Select an option

Save ohadravid/25f0a4de72bd54042351541ef061ac49 to your computer and use it in GitHub Desktop.
from contextlib import closing
from threading import Thread
import zmq
import time
from pathlib import Path
class AbstractWidget:
def __init__(self):
self.config = Path("config.json").read_text()
class FooWidget(AbstractWidget):
def __init__(self):
super().__init__()
self.ctx = zmq.Context.instance()
self.consumer: zmq.Socket = self.ctx.socket(zmq.PULL)
self.should_exit = False
def run(self):
while self.should_exit is False:
time.sleep(1)
self.consumer.close()
def close(self):
if self.should_exit is False:
self.should_exit = True
class FooBarWidget(FooWidget):
def __init__(self):
self.ctx = zmq.Context.instance()
self.publisher: zmq.Socket = self.ctx.socket(zmq.PUSH)
self._init()
def _init(self):
def worker_thread_start():
FooWidget.__init__(self)
self.run()
worker_thread = Thread(target=worker_thread_start, daemon=True)
worker_thread.start()
def bar(self):
pass
def close(self):
super().close()
self.publisher.close()
# Will randomly fail with `AttributeError: 'FooBarWidget' object has no attribute 'should_exit'`.
# Sleeping a little will "fix" it.
def test_foobar():
f = FooBarWidget()
with closing(f):
# time.sleep(0.01)
assert False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment