本文整理汇总了Python中pyon.event.event.EventSubscriber类的典型用法代码示例。如果您正苦于以下问题:Python EventSubscriber类的具体用法?Python EventSubscriber怎么用?Python EventSubscriber使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventSubscriber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: InstrumentAgentEventSubscribers
class InstrumentAgentEventSubscribers(object):
"""
Create subscribers for agent and driver events.
"""
log.info("Start event subscribers")
def __init__(self, instrument_agent_resource_id = None):
# Start event subscribers, add stop to cleanup.
self.no_events = None
self.events_received = []
self.async_event_result = AsyncResult()
self.event_subscribers = []
def consume_event(*args, **kwargs):
log.debug('#**#**# Event subscriber (consume_event) recieved ION event: args=%s, kwargs=%s, event=%s.',
str(args), str(kwargs), str(args[0]))
log.debug("self.no_events = " + str(self.no_events))
log.debug("self.event_received = " + str(self.events_received))
self.events_received.append(args[0])
if self.no_events and self.no_events == len(self.events_received):
log.debug("CALLING self.async_event_result.set()")
self.async_event_result.set()
self.event_subscribers = EventSubscriber(
event_type='ResourceAgentEvent', callback=consume_event,
origin=instrument_agent_resource_id)
self.event_subscribers.start()
self.event_subscribers._ready_event.wait(timeout=5)
示例2: on_start
def on_start(self):
#print ">>>>>>>>>>>>>>>>>>>>>> MPL CFG = ", self.CFG
self.pubsub_management = PubsubManagementServiceProcessClient(process=self)
self.ssclient = SchedulerServiceProcessClient(process=self)
self.rrclient = ResourceRegistryServiceProcessClient(process=self)
self.data_retriever_client = DataRetrieverServiceProcessClient(process=self)
self.dsm_client = DatasetManagementServiceProcessClient(process=self)
self.pubsub_client = PubsubManagementServiceProcessClient(process = self)
self.stream_info = self.CFG.get_safe('process.publish_streams',{})
self.stream_names = self.stream_info.keys()
self.stream_ids = self.stream_info.values()
if not self.stream_names:
raise BadRequest('MPL Transform has no output streams.')
graph_time_periods= self.CFG.get_safe('graph_time_periods')
# If this is meant to be an event driven process, schedule an event to be generated every few minutes/hours
self.event_timer_interval = self.CFG.get_safe('graph_gen_interval')
if self.event_timer_interval:
event_origin = "Interval_Timer_Matplotlib"
sub = EventSubscriber(event_type="ResourceEvent", callback=self.interval_timer_callback, origin=event_origin)
sub.start()
self.interval_timer_id = self.ssclient.create_interval_timer(start_time="now" , interval=self._str_to_secs(self.event_timer_interval),
event_origin=event_origin, event_subtype="")
super(VizTransformMatplotlibGraphs,self).on_start()
示例3: NotificationSubscription
class NotificationSubscription(object):
"""
Ties a notification's info to it's event subscriber
"""
def __init__(self, notification_request=None, callback=None):
self._res_obj = notification_request # The Notification Request Resource Object
self.subscriber = EventSubscriber( origin=notification_request.origin,
origin_type = notification_request.origin_type,
event_type=notification_request.event_type,
sub_type=notification_request.event_subtype,
callback=callback)
self.notification_subscription_id = None
def set_notification_id(self, id_=None):
"""
Set the notification id of the notification object
@param notification id
"""
self.notification_subscription_id = id_
def activate(self):
"""
Start subscribing
"""
self.subscriber.start()
def deactivate(self):
"""
Stop subscribing
"""
self.subscriber.stop()
示例4: test_pub_on_different_subtypes
def test_pub_on_different_subtypes(self):
ar = event.AsyncResult()
gq = queue.Queue()
self.count = 0
def cb(event, *args, **kwargs):
self.count += 1
gq.put(event)
if event.description == "end":
ar.set()
sub = EventSubscriber(event_type="ResourceModifiedEvent", sub_type="st1", callback=cb)
sub.activate()
pub1 = EventPublisher(event_type="ResourceModifiedEvent")
pub2 = EventPublisher(event_type="ContainerLifecycleEvent")
pub1.publish_event(origin="two", sub_type="st2", description="2")
pub2.publish_event(origin="three", sub_type="st1", description="3")
pub1.publish_event(origin="one", sub_type="st1", description="1")
pub1.publish_event(origin="four", sub_type="st1", description="end")
ar.get(timeout=5)
sub.deactivate()
res = []
for x in xrange(self.count):
res.append(gq.get(timeout=5))
self.assertEquals(len(res), 2)
self.assertEquals(res[0].description, "1")
示例5: start
def start(self):
log.debug("GovernanceController starting ...")
self.enabled = CFG.get_safe('interceptor.interceptors.governance.config.enabled', False)
log.info("GovernanceInterceptor enabled: %s" % str(self.enabled))
self.resource_policy_event_subscriber = None
self.service_policy_event_subscriber = None
#containers default to not Org Boundary and ION Root Org
self._is_container_org_boundary = CFG.get_safe('container.org_boundary',False)
self._container_org_name = CFG.get_safe('container.org_name', CFG.get_safe('system.root_org', 'ION'))
self._container_org_id = None
self._system_root_org_name = CFG.get_safe('system.root_org', 'ION')
self._is_root_org_container = (self._container_org_name == self._system_root_org_name)
if self.enabled:
config = CFG.get_safe('interceptor.interceptors.governance.config')
self.initialize_from_config(config)
self.resource_policy_event_subscriber = EventSubscriber(event_type="ResourcePolicyEvent", callback=self.resource_policy_event_callback)
self.resource_policy_event_subscriber.start()
self.service_policy_event_subscriber = EventSubscriber(event_type="ServicePolicyEvent", callback=self.service_policy_event_callback)
self.service_policy_event_subscriber.start()
self.rr_client = ResourceRegistryServiceProcessClient(node=self.container.node, process=self.container)
self.policy_client = PolicyManagementServiceProcessClient(node=self.container.node, process=self.container)
示例6: test_create_single_timer
def test_create_single_timer(self):
# test creating a new timer that is one-time-only
# create the timer resource
# create the event listener
# call scheduler to set the timer
# create then cancel the timer, verify that event is not received
# create the timer resource
# create the event listener
# call scheduler to set the timer
# call scheduler to cancel the timer
# wait until after expiry to verify that event is not sent
self.single_timer_count = 0
event_origin = "Time of Day"
sub = EventSubscriber(event_type="ResourceEvent", callback=self.single_timer_call_back, origin=event_origin)
sub.start()
# Time out in 3 seconds
now = datetime.datetime.utcnow() + timedelta(seconds=3)
times_of_day =[{'hour': str(now.hour),'minute' : str(now.minute), 'second':str(now.second) }]
ss = SchedulerService()
id = ss.create_time_of_day_timer(times_of_day=times_of_day, expires=time.time()+25200+60, event_origin=event_origin, event_subtype="")
self.assertEqual(type(id), str)
ss.cancel_timer(id)
gevent.sleep(5)
# Validate the event is not sent
self.assertEqual(self.single_timer_count, 0)
示例7: test_qc_events
def test_qc_events(self):
ph = ParameterHelper(self.dataset_management, self.addCleanup)
pdict_id = ph.create_qc_pdict()
stream_def_id = self.pubsub_management.create_stream_definition('qc stream def', parameter_dictionary_id=pdict_id)
self.addCleanup(self.pubsub_management.delete_stream_definition, stream_def_id)
stream_id, route = self.pubsub_management.create_stream('qc stream', exchange_point=self.exchange_point_name, stream_definition_id=stream_def_id)
self.addCleanup(self.pubsub_management.delete_stream, stream_id)
ingestion_config_id = self.get_ingestion_config()
dataset_id = self.create_dataset(pdict_id)
config = DotDict()
self.ingestion_management.persist_data_stream(stream_id=stream_id, ingestion_configuration_id=ingestion_config_id, dataset_id=dataset_id, config=config)
self.addCleanup(self.ingestion_management.unpersist_data_stream, stream_id, ingestion_config_id)
publisher = StandaloneStreamPublisher(stream_id, route)
rdt = RecordDictionaryTool(stream_definition_id=stream_def_id)
rdt['time'] = np.arange(10)
rdt['temp'] = np.arange(10) * 3
verified = Event()
def verification(event, *args, **kwargs):
self.assertEquals(event.qc_parameter, 'temp_qc')
self.assertEquals(event.temporal_value, 7)
verified.set()
es = EventSubscriber(event_type=OT.ParameterQCEvent, origin=dataset_id, callback=verification, auto_delete=True)
es.start()
self.addCleanup(es.stop)
publisher.publish(rdt.to_granule())
self.assertTrue(verified.wait(10))
示例8: test_cancel_single_timer
def test_cancel_single_timer(self):
# test creating a new timer that is one-time-only
# create the timer resource
# create the event listener
# call scheduler to set the timer
# create then cancel the timer, verify that event is not received
# create the timer resource
# create the event listener
# call scheduler to set the timer
# call scheduler to cancel the timer
# wait until after expiry to verify that event is not sent
self.single_timer_count = 0
event_origin = "Time_of_Day"
sub = EventSubscriber(event_type="TimerEvent", callback=self.single_timer_callback, origin=event_origin)
sub.start()
self.addCleanup(sub.stop)
now = datetime.datetime.utcnow() + timedelta(seconds=3)
times_of_day =[{'hour': str(now.hour),'minute' : str(now.minute), 'second':str(now.second) }]
id = self.ssclient.create_time_of_day_timer(times_of_day=times_of_day, expires=self.now_utc()+3, event_origin=event_origin, event_subtype="test")
self.assertEqual(type(id), str)
self.ssclient.cancel_timer(id)
gevent.sleep(3)
# Validate the event is not generated
self.assertEqual(self.single_timer_count, 0, "Invalid number of timeouts generated. Number of timeout: %d Expected timeout: 0 Timer id: %s " %(self.single_timer_count, id))
示例9: start_DeviceStatusAlertEvent_subscriber
def start_DeviceStatusAlertEvent_subscriber(value_id, sub_type):
"""
@return async_event_result Use it to wait for the expected event
"""
event_type = "DeviceStatusAlertEvent"
async_event_result = AsyncResult()
def consume_event(evt, *args, **kwargs):
log.info('DeviceStatusAlertEvent_subscriber received evt: %s', str(evt))
if evt.type_ != event_type or \
evt.value_id != value_id or \
evt.sub_type != sub_type:
return
async_event_result.set(evt)
kwargs = dict(event_type=event_type,
callback=consume_event,
origin=self.p_root.platform_device_id,
sub_type=sub_type)
sub = EventSubscriber(**kwargs)
sub.start()
log.info("registered DeviceStatusAlertEvent subscriber: %s", kwargs)
self._event_subscribers.append(sub)
sub._ready_event.wait(timeout=self._receive_timeout)
return async_event_result
示例10: test_create_interval_timer_with_end_time
def test_create_interval_timer_with_end_time(self):
# create the interval timer resource
# create the event listener
# call scheduler to set the timer
# receive a few intervals, validate that arrival time is as expected
# Validate no more events are published after end_time expires
# Validate the timer was canceled after the end_time expires
self.interval_timer_count_2 = 0
self.interval_timer_sent_time_2 = 0
self.interval_timer_received_time_2 = 0
self.interval_timer_interval_2 = 3
event_origin = "Interval_Timer_2"
sub = EventSubscriber(event_type="TimerEvent", callback=self.interval_timer_callback_with_end_time, origin=event_origin)
sub.start()
self.addCleanup(sub.stop)
start_time = self.now_utc()
self.interval_timer_end_time_2 = start_time + 7
id = self.ssclient.create_interval_timer(start_time="now" , interval=self.interval_timer_interval_2,
end_time=self.interval_timer_end_time_2,
event_origin=event_origin, event_subtype="")
self.interval_timer_sent_time_2 = datetime.datetime.utcnow()
self.assertEqual(type(id), str)
# Wait until all events are published
gevent.sleep((self.interval_timer_end_time_2 - start_time) + self.interval_timer_interval_2 + 1)
# Validate the number of events generated
self.assertEqual(self.interval_timer_count_2, 2, "Invalid number of timeouts generated. Number of event: %d Expected: 2 Timer id: %s " %(self.interval_timer_count_2, id))
# Validate the timer was canceled after the end_time is expired
with self.assertRaises(BadRequest):
self.ssclient.cancel_timer(id)
示例11: test_quit_stops_timers
def test_quit_stops_timers(self):
ar = AsyncResult()
def cb(*args, **kwargs):
ar.set(args)
self.interval_timer_count += 1
event_origin = "test_quitter"
sub = EventSubscriber(event_type="TimerEvent", callback=cb, origin=event_origin)
sub.start()
self.addCleanup(sub.stop)
tid = self.ssclient.create_interval_timer(start_time="now",
interval=1,
event_origin=event_origin)
# wait until at least one scheduled message
ar.get(timeout=5)
# shut it down!
p = self.container.proc_manager.procs_by_name['scheduler']
self.container.terminate_process(p.id)
# assert empty
self.assertEquals(p.schedule_entries, {})
示例12: test_instrument_device_metadata_notification_l4_ci_sa_rq_145_323
def test_instrument_device_metadata_notification_l4_ci_sa_rq_145_323(self):
"""
Instrument management shall update physical resource metadata when change occurs
For example, when there is a change of state.
note from maurice 2012-05-18: consider this to mean a change of stored RR data
"""
inst_obj = any_old(RT.InstrumentDevice)
instrument_device_id, _ = self.RR.create(inst_obj)
self.received_event = AsyncResult()
#Create subscribers for agent and driver events.
def consume_event(*args, **kwargs):
self.received_event.set(True)
log.info("L4-CI-SA-RQ-323")
log.info("L4-CI-SA-RQ-145")
event_sub = EventSubscriber(event_type="ResourceModifiedEvent", callback=consume_event)
event_sub.start()
inst_obj = self.RR.read(instrument_device_id)
inst_obj.description = "brand new description"
self.RR.update(inst_obj)
#wait for event
result = self.received_event.get(timeout=10)
event_sub.stop()
self.assertTrue(result)
示例13: test_ingestion_failover
def test_ingestion_failover(self):
stream_id, route, stream_def_id, dataset_id = self.make_simple_dataset()
self.start_ingestion(stream_id, dataset_id)
event = Event()
def cb(*args, **kwargs):
event.set()
sub = EventSubscriber(event_type="ExceptionEvent", callback=cb, origin="stream_exception")
sub.start()
self.publish_fake_data(stream_id, route)
self.wait_until_we_have_enough_granules(dataset_id, 40)
file_path = DatasetManagementService._get_coverage_path(dataset_id)
master_file = os.path.join(file_path, '%s_master.hdf5' % dataset_id)
with open(master_file, 'w') as f:
f.write('this will crash HDF')
self.publish_hifi(stream_id, route, 5)
self.assertTrue(event.wait(10))
sub.stop()
示例14: _expect_from_root
def _expect_from_root(self, p_root):
"""
Start an event subscriber to the given root platform.
To be called before any action that triggers publications from the root
platform. It sets self._wait_root_event to a function to be called to
wait for the event.
"""
async_result = AsyncResult()
# subscribe:
event_type = "DeviceAggregateStatusEvent"
def consume_event(evt, *args, **kwargs):
async_result.set(evt)
sub = EventSubscriber(event_type=event_type,
origin=p_root.platform_device_id,
callback=consume_event)
sub.start()
self._data_subscribers.append(sub)
sub._ready_event.wait(timeout=CFG.endpoint.receive.timeout)
log.debug("registered for DeviceAggregateStatusEvent")
# set new wait function:
def wait():
root_evt = async_result.get(timeout=CFG.endpoint.receive.timeout)
return root_evt
self._wait_root_event = wait
示例15: test_create_forever_interval_timer
def test_create_forever_interval_timer(self):
# Test creating interval timer that runs forever
self.interval_timer_count = 0
self.interval_timer_sent_time = 0
self.interval_timer_received_time = 0
self.interval_timer_interval = 3
event_origin = "Interval Timer Forever"
sub = EventSubscriber(event_type="ResourceEvent", callback=self.interval_timer_callback, origin=event_origin)
sub.start()
id = self.ssclient.create_interval_timer(start_time= self.now_utc(), interval=self.interval_timer_interval,
end_time=-1,
event_origin=event_origin, event_subtype=event_origin)
self.interval_timer_sent_time = datetime.datetime.utcnow()
self.assertEqual(type(id), str)
# Wait for 4 events to be published
gevent.sleep((self.interval_timer_interval * 4) + 1)
self.ssclient.cancel_timer(id)
# Validate the timer id is invalid once it has been canceled
with self.assertRaises(BadRequest):
self.ssclient.cancel_timer(id)
# Validate events are not generated after canceling the timer
self.assertEqual(self.interval_timer_count, 4)