hephy-dd / comet

COMET - Control and Measurement Toolkit
GNU General Public License v3.0
2 stars 2 forks source link

Process event handling: revisited #43

Closed arnobaer closed 4 years ago

arnobaer commented 4 years ago

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!