def _actor_loop(self): """ The actor's event loop. This is the method that will be executed by the thread or greenlet. """ self.on_start() while self._actor_runnable: message = self.actor_inbox.get() try: response = self._handle_receive(message) if 'reply_to' in message: message['reply_to'].set(response) except Exception: if 'reply_to' in message: _logger.debug( 'Exception returned from %s to caller:' % self, exc_info=_sys.exc_info()) message['reply_to'].set_exception() else: self._handle_failure(*_sys.exc_info()) except BaseException: exception_value = _sys.exc_info()[1] _logger.debug( '%s in %s. Stopping all actors.' % (repr(exception_value), self)) self._stop() _ActorRegistry.stop_all()
def _actor_loop(self): """ The actor's event loop. This is the method that will be executed by the thread or greenlet. """ try: self.on_start() except Exception: self._handle_failure(*_sys.exc_info()) while self._actor_runnable: message = self.actor_inbox.get() try: response = self._handle_receive(message) if 'reply_to' in message: message['reply_to'].set(response) except Exception: if 'reply_to' in message: _logger.debug('Exception returned from %s to caller:' % self, exc_info=_sys.exc_info()) message['reply_to'].set_exception() else: self._handle_failure(*_sys.exc_info()) try: self.on_failure(*_sys.exc_info()) except Exception: self._handle_failure(*_sys.exc_info()) except BaseException: exception_value = _sys.exc_info()[1] _logger.debug('%s in %s. Stopping all actors.' % (repr(exception_value), self)) self._stop() _ActorRegistry.stop_all()
def _run(self): """ The actor's main method. :class:`pykka.gevent.GeventActor` expects this method to be named :meth:`_run`. :class:`ThreadingActor` expects this method to be named :meth:`run`. """ self.on_start() while self._actor_runnable: try: message = self.actor_inbox.get(False) _logger.debug('Actor {} got message {}'.format(self, message)) try: response = self._handle_receive(message) if 'reply_to' in message: message['reply_to'].set(response) except Exception as exception: if 'reply_to' in message: _logger.debug('Exception returned from %s to caller:' % self, exc_info=_sys.exc_info()) message['reply_to'].set_exception(exception) else: self._handle_failure(*_sys.exc_info()) except BaseException as exception: exception_value = _sys.exc_info()[1] _logger.debug('%s in %s. Stopping all actors.' % (repr(exception_value), self)) self.stop() _ActorRegistry.stop_all() except _queue.Empty: pass
def scan_command(directory, tagfile='STAG.sqlite', verbose=False): """Scan a directory tree for definitions and references.""" init_logging(verbose) with StorageManager(Storage(tagfile)) as s: s.clear_defs() storage = StorageActor.start(s) parser_map = [ (list(p.patterns()), ParserActor.start(p.create_parser(), storage)) for p in parser_plugins()] dispatcher = DispatcherActor.start(parser_map) def dispatch_file(args): "Send results of os.walk to the dispatcher." dirpath, _, filenames = args for fname in filenames: dispatcher.tell({ 'command': 'dispatch', 'filename': os.path.join(dirpath, fname) }) emap(dispatch_file, os.walk(directory)) # Shut everything down. ActorRegistry.stop_all()
def _run(self): """ The actor's main method. :class:`GeventActor <pykka.gevent.GeventActor>` expects this method to be named :meth:`_run`. :class:`ThreadingActor` expects this method to be named :meth:`run`. """ self.on_start() while self._actor_runnable: message = self.actor_inbox.get() try: response = self._handle_receive(message) if 'reply_to' in message: message['reply_to'].set(response) except Exception: if 'reply_to' in message: _logger.debug('Exception returned from %s to caller:' % self, exc_info=_sys.exc_info()) message['reply_to'].set_exception() else: self._handle_failure(*_sys.exc_info()) except BaseException: exception_value = _sys.exc_info()[1] _logger.debug('%s in %s. Stopping all actors.' % (repr(exception_value), self)) self._stop() _ActorRegistry.stop_all()
def stop_all_actors(): num_actors = len(ActorRegistry.get_all()) while num_actors: logger.debug(u'Seeing %d actor and %d non-actor thread(s): %s', num_actors, threading.active_count() - num_actors, ', '.join([t.name for t in threading.enumerate()])) logger.debug(u'Stopping %d actor(s)...', num_actors) ActorRegistry.stop_all() num_actors = len(ActorRegistry.get_all()) logger.debug(u'All actors stopped.')
def stop(self): logger.info("exiting ...") logger.info("waiting for actors to stop ...") try: ActorRegistry.stop_all(timeout=10) except Exception as ex: logger.info("warning - actors failed to stop cleanly") logger.info("stopping web server ...") stop_web() logger.info("finished")
def stop_remaining_actors(): num_actors = len(ActorRegistry.get_all()) while num_actors: logger.error( u'There are actor threads still running, this is probably a bug') logger.debug(u'Seeing %d actor and %d non-actor thread(s): %s', num_actors, threading.active_count() - num_actors, ', '.join([t.name for t in threading.enumerate()])) logger.debug(u'Stopping %d actor(s)...', num_actors) ActorRegistry.stop_all() num_actors = len(ActorRegistry.get_all()) logger.debug(u'All actors stopped.')
def test_runs_parallel(self): self.actor = CoroutineActor.start().proxy() first = self.actor.infinite_loop_count.get() self.actor.loop_infinitely(0).get() second = self.actor.infinite_loop_count.get() self.actor.other_method().get() called = self.actor.other_called.get() third = self.actor.infinite_loop_count.get() self.assertTrue(called) self.assertLess(first, second) self.assertLess(second, third) ActorRegistry.stop_all()
def _actor_loop(self): """ The actor's event loop. This is the method that will be executed by the thread or greenlet. """ try: self.on_start() except Exception: self._handle_failure(*sys.exc_info()) while not self.actor_stopped.is_set(): message = self.actor_inbox.get() reply_to = None try: reply_to = message.pop('pykka_reply_to', None) response = self._handle_receive(message) if reply_to: reply_to.set(response) except Exception: if reply_to: logger.debug( 'Exception returned from %s to caller:' % self, exc_info=sys.exc_info()) reply_to.set_exception() else: self._handle_failure(*sys.exc_info()) try: self.on_failure(*sys.exc_info()) except Exception: self._handle_failure(*sys.exc_info()) except BaseException: exception_value = sys.exc_info()[1] logger.debug( '%s in %s. Stopping all actors.' % (repr(exception_value), self)) self._stop() ActorRegistry.stop_all() while not self.actor_inbox.empty(): msg = self.actor_inbox.get() reply_to = msg.pop('pykka_reply_to', None) if reply_to: if msg.get('command') == 'pykka_stop': reply_to.set(None) else: reply_to.set_exception(ActorDeadError( '%s stopped before handling the message' % self.actor_ref))
def _actor_loop(self): """ The actor's event loop. This is the method that will be executed by the thread or greenlet. """ try: self.on_start() except Exception: self._handle_failure(*sys.exc_info()) while not self.actor_stopped.is_set(): message = self.actor_inbox.get() reply_to = None try: reply_to = message.pop('pykka_reply_to', None) response = self._handle_receive(message) if reply_to: reply_to.set(response) except Exception: if reply_to: logger.debug('Exception returned from %s to caller:' % self, exc_info=sys.exc_info()) reply_to.set_exception() else: self._handle_failure(*sys.exc_info()) try: self.on_failure(*sys.exc_info()) except Exception: self._handle_failure(*sys.exc_info()) except BaseException: exception_value = sys.exc_info()[1] logger.debug('%s in %s. Stopping all actors.' % (repr(exception_value), self)) self._stop() ActorRegistry.stop_all() while not self.actor_inbox.empty(): msg = self.actor_inbox.get() reply_to = msg.pop('pykka_reply_to', None) if reply_to: if msg.get('command') == 'pykka_stop': reply_to.set(None) else: reply_to.set_exception( ActorDeadError( '%s stopped before handling the message' % self.actor_ref))
def run(pool_size, *ips): # Start resolvers resolvers = [Resolver.start_proxy() for _ in range(pool_size)] # Distribute work by mapping IPs to resolvers (not blocking) hosts = [] for i, ip in enumerate(ips): hosts.append(resolvers[i % len(resolvers)].resolve(ip)) # Gather results (blocking) ip_to_host = zip(ips, get_all(hosts)) pprint(ip_to_host) # Clean up ActorRegistry.stop_all()
def run(pool_size, *ips): # Start resolvers resolvers = [Resolver.start().proxy() for _ in range(pool_size)] # Distribute work by mapping IPs to resolvers (not blocking) hosts = [] for i, ip in enumerate(ips): hosts.append(resolvers[i % len(resolvers)].resolve(ip)) # Gather results (blocking) ip_to_host = zip(ips, get_all(hosts)) pprint(ip_to_host) # Clean up ActorRegistry.stop_all()
def test_stop_all_stops_last_started_actor_first_if_blocking( self, mock_method): stopped_actors = [] started_actors = [mock.Mock(name=i) for i in range(3)] started_actors[0].stop.side_effect = lambda *a, **kw: \ stopped_actors.append(started_actors[0]) started_actors[1].stop.side_effect = lambda *a, **kw: \ stopped_actors.append(started_actors[1]) started_actors[2].stop.side_effect = lambda *a, **kw: \ stopped_actors.append(started_actors[2]) ActorRegistry.get_all.return_value = started_actors ActorRegistry.stop_all(block=True) self.assertEqual(stopped_actors[0], started_actors[2]) self.assertEqual(stopped_actors[1], started_actors[1]) self.assertEqual(stopped_actors[2], started_actors[0])
def test_all_actors_can_be_stopped_through_registry(self): self.assertEquals(9, len(ActorRegistry.get_all())) ActorRegistry.stop_all(block=True) self.assertEquals(0, len(ActorRegistry.get_all()))
#! /usr/bin/env python from pykka.actor import ThreadingActor from pykka.registry import ActorRegistry class Adder(ThreadingActor): def add_one(self, i): print '%s is increasing %d' % (self, i) return i + 1 class Bookkeeper(ThreadingActor): def __init__(self, adder): self.adder = adder def count_to(self, target): i = 0 while i < target: i = self.adder.add_one(i).get() print '%s got %d back' % (self, i) if __name__ == '__main__': adder = Adder.start().proxy() bookkeeper = Bookkeeper.start(adder).proxy() bookkeeper.count_to(10).get() ActorRegistry.stop_all()
def tearDown(self): ActorRegistry.stop_all() self.mockedSocket.stop()
def tearDown(self): self.mocked_server.stop() ActorRegistry.stop_all() shutil.rmtree(self.share_dir)
def tearDown(self): self.log_handler.close() ActorRegistry.stop_all()
def exit_status(exit_code): settings_listener.stop() ActorRegistry.stop_all() return exit_code
def OnExit(self): ActorRegistry.stop_all()
def tearDown(self): ActorRegistry.stop_all() shutil.rmtree(self.tmp_share_dir)
def tearDown(self): ActorRegistry.stop_all()
def test_direct_plain_attribute_access(): actor = AnActor.start().proxy() for i in range(10000): foo = actor.foo.get() def test_direct_callable_attribute_access(): actor = AnActor.start().proxy() for i in range(10000): actor.func().get() def test_traversible_plain_attribute_access(): actor = AnActor.start().proxy() for i in range(10000): foo = actor.bar.baz.get() def test_traversible_callable_attribute_access(): actor = AnActor.start().proxy() for i in range(10000): actor.bar.func().get() if __name__ == '__main__': try: time_it(test_direct_plain_attribute_access) time_it(test_direct_callable_attribute_access) time_it(test_traversible_plain_attribute_access) time_it(test_traversible_callable_attribute_access) finally: ActorRegistry.stop_all()