Skip to content

Process Use Patterns

Michael Meisinger edited this page Jun 18, 2015 · 4 revisions

The following patterns are commonly used within an ION process. self refers to the process instance, e.g. within on_start() or similar.

Accessing process config

    entry1 = self.CFG.get_safe("myproc.context.entry1")
    entry2 = self.CFG.get_safe("myproc.context.entry2", "default")
    entry3 = self.CFG.get_safe("myproc.context.entry3") is True

Any process specific config modifications are contained within self.CFG

Creating another greenlet (thread)

def on_start():
    self.thread_quit = Event()
    self._process.thread_manager.spawn(self.thread_start, arg1, arg2)

def on_quit():
    self.thread_quit.set()

def thread_start(arg1, arg2):
    while not self.thread_quit.wait(timeout=1.0):
        pass

Using the process thread manager ensures that the container can clean up the greenlets if something abnornal happens.

Event subscriber

def on_start():
    self.event_sub = ProcessEventSubscriber(event_type=OT.ResourceEvent, process=self)
    self.add_endpoint(self.event_sub)

A ProcessEventSubscriber executes event callbacks within the process control greenlet, which sequentializes process RPC (e.g. service calls) and events from all messaging listeners.

A regular EventSubscriber executes callbacks in a concurrent greenlet.

Event publisher

Stream subscriber

def on_start():
    self.subscriber = StreamSubscriber(process=self, exchange_name=queue_name, callback=self.on_msg)
    self.add_endpoint(self.subscriber)

def on_msg(self, msg, stream_route, stream_id):
    pass

Clone this wiki locally