Example #1
0
    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()
Example #2
0
    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()
Example #3
0
    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
Example #4
0
File: app.py Project: abingham/stag
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()
Example #5
0
    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()
Example #6
0
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.')
Example #7
0
 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")
Example #8
0
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.')
Example #9
0
 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()
Example #10
0
    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))
Example #11
0
    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))
Example #12
0
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()
Example #13
0
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()
Example #14
0
    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])
Example #15
0
    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])
Example #16
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()))
Example #17
0
#! /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()
Example #18
0
 def tearDown(self):
     ActorRegistry.stop_all()
     self.mockedSocket.stop()
Example #19
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()))
Example #20
0
 def tearDown(self):
     self.mocked_server.stop()
     ActorRegistry.stop_all()
     shutil.rmtree(self.share_dir)
Example #21
0
 def tearDown(self):
     self.log_handler.close()
     ActorRegistry.stop_all()
Example #22
0
def exit_status(exit_code):
    settings_listener.stop()
    ActorRegistry.stop_all()
    return exit_code
Example #23
0
 def tearDown(self):
     self.log_handler.close()
     ActorRegistry.stop_all()
Example #24
0
 def OnExit(self):
     ActorRegistry.stop_all()
Example #25
0
 def tearDown(self):
     ActorRegistry.stop_all()
     shutil.rmtree(self.tmp_share_dir)
Example #26
0
def exit_status(exit_code):
  settings_listener.stop()
  ActorRegistry.stop_all()
  return exit_code
Example #27
0
 def tearDown(self):
     ActorRegistry.stop_all()
Example #28
0
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()
Example #29
0
 def tearDown(self):
     ActorRegistry.stop_all()
 def OnExit(self):
     ActorRegistry.stop_all()