Introducing an event handler for convenient use of callbacks from within processes (threads).
Depending on the thread context an event callback is either called in the same thread the process was created in (usually the main thread) or a PyQt5 signal is emitted from within a worker thread.
Callbacks should be accessible as dictionary and also as attribute for convenience.
def on_reading(data):
print(data)
def on_finished():
print("finished.")
def run(p):
while p.running:
p.events.reading([42]) # access as attribute
p.events["finished"]() # access as dict item
app = comet.Application()
p = comet.Process(target=run, events=dict(reading=print)) # initial assign as dict
p.events["reading"] = on_reading # (re-)assign as dict item
p.events.finished = on_finished # assign as attribute
p.start()
app.run() # events require event loop to work!
Introducing an event handler for convenient use of callbacks from within processes (threads).
Depending on the thread context an event callback is either called in the same thread the process was created in (usually the main thread) or a PyQt5 signal is emitted from within a worker thread.
Callbacks should be accessible as dictionary and also as attribute for convenience.