I have a scenario where I am using the python circuits framework to dynamically create new components which have their own channel. I would like to be notified when ALL of the created channels have fired a particular event.
I have tried using the success event but this fires on each channel independently, so I get one per channel. It makes sense for me to use different channels as the same tasks are being carried out on different data sets.
My current solution is to record the created channel names and then listen for the finishing event ("boom" below) and remove the firing channel from a list. When the list is empty I can stop. An example of this is below. It works, but I feel there should be a more elegant way of joining these channels once they have finished.
import time
import sys
from circuits import Component, Event
from circuits.core.debugger import Debugger
class boom(Event):
"boom event"
class Start(Component):
def __init__(self, channel="*"):
super(Start, self).__init__(channel=channel)
self._boom_channels = []
return
def started(self, *args):
for i in [1,2]:
channel = 'channel_{}'.format(i)
self._boom_channels.append(channel)
new = Middle(channel=channel).register(self)
def boom(self, event, *args):
new_chans = set(self._boom_channels) - set(event.channels)
self._boom_channels = list(new_chans)
print self._boom_channels
if not self._boom_channels:
sys.exit()
class Middle(Component):
def __init__(self, channel="*"):
super(Middle, self).__init__(channel=channel)
time.sleep(2)
self.fire(boom())
return
if __name__ == '__main__':
(Start() + Debugger()).run()