Пример #1
0
def test_all_actors_can_be_stopped_through_registry(a_actor_refs,
                                                    b_actor_refs):
    assert len(ActorRegistry.get_all()) == 8

    ActorRegistry.stop_all(block=True)

    assert len(ActorRegistry.get_all()) == 0
Пример #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 not self.actor_stopped.is_set():
            envelope = self.actor_inbox.get()
            try:
                response = self._handle_receive(envelope.message)
                if envelope.reply_to is not None:
                    envelope.reply_to.set(response)
            except Exception:
                if envelope.reply_to is not None:
                    logger.info(
                        'Exception returned from {} to caller:'.format(self),
                        exc_info=sys.exc_info(),
                    )
                    envelope.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(
                    '{!r} in {}. Stopping all actors.'.format(
                        exception_value, self
                    )
                )
                self._stop()
                ActorRegistry.stop_all()

        while not self.actor_inbox.empty():
            envelope = self.actor_inbox.get()
            if envelope.reply_to is not None:
                if isinstance(envelope.message, messages._ActorStop):
                    envelope.reply_to.set(None)
                else:
                    envelope.reply_to.set_exception(
                        exc_info=(
                            ActorDeadError,
                            ActorDeadError(
                                '{} stopped before handling the message'.format(
                                    self.actor_ref
                                )
                            ),
                            None,
                        )
                    )
Пример #3
0
    def test_fail(self):   # pylint: disable=R0201
        """ Test closing stream when fail. """
        def buggy_write(_):
            """ A buggy writable """
            raise ValueError()
        c = Collector.start(self.controller,
                            flexmock(write=buggy_write)
                            .should_receive('close').once().mock())

        c.tell(Record(None, 'Fail'))
        ActorRegistry.stop_all()
Пример #4
0
def test_register_get_by_filter(actor_class):
    actor_ref = actor_class().start()
    # 已经完成注册啦了.
    # step1 测试 urn查询
    assert ActorRegistry.get_by_urn(actor_ref.actor_urn) == actor_ref
    # step2 测试类名查询
    assert ActorRegistry.get_by_class(actor_ref.actor_class)[0] == actor_ref
    # # step3 测试类名字符查询
    assert ActorRegistry.get_by_class_name(
        'CustomThreadingActor')[0] == actor_ref
    ActorRegistry.stop_all()
Пример #5
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():
            envelope = self.actor_inbox.get()
            try:
                response = self._handle_receive(envelope.message)
                if envelope.reply_to is not None:
                    envelope.reply_to.set(response)
            except Exception:
                if envelope.reply_to is not None:
                    logger.info(
                        'Exception returned from {} to caller:'.format(self),
                        exc_info=sys.exc_info(),
                    )
                    envelope.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('{!r} in {}. Stopping all actors.'.format(
                    exception_value, self))
                self._stop()
                ActorRegistry.stop_all()

        while not self.actor_inbox.empty():
            envelope = self.actor_inbox.get()
            if envelope.reply_to is not None:
                if isinstance(envelope.message, messages._ActorStop):
                    envelope.reply_to.set(None)
                else:
                    envelope.reply_to.set_exception(exc_info=(
                        ActorDeadError,
                        ActorDeadError('{} stopped before handling the message'
                                       .format(self.actor_ref)),
                        None,
                    ))
Пример #6
0
    def test_work(self):
        class Worker(object):
            def __init__(self, arg):
                self.arg = arg

            def work_on(self, task):
                print task
                return task

        flexmock(Worker).should_call('work_on').once().with_args(1)
        cr = Crawler.start(self.controller, self.tasksource, self.collector,
                           Worker, self.initargs)
        cr.tell(Task(None, 1))
        cr.stop()
        ActorRegistry.stop_all()
Пример #7
0
def solve(study: Study) -> Study:
    waiter = Waiter(wait_ms=2)

    dispatcher = [create_dispatcher(name, node) for name, node in study.nodes.items()]
    for d in dispatcher:
        d.tell(Start())

    waiter.wait()

    nodes = {}
    for d in dispatcher:
        name, (cons, prod, borders) = d.ask(Next())
        nodes[name] = NodeQuantity(consumptions=cons, productions=prod, borders=borders)

    ActorRegistry.stop_all()
    return Study(nodes=nodes)
Пример #8
0
def test_register_stop_all(actor_class):
    """
    终止所有的actor
    :param actor_class:
    :return:
    """
    actor_class().start()
    assert ActorRegistry.stop_all()
Пример #9
0
 def test_write(self):  # pylint: disable=R0201
     """ Test writing to a writable.  """
     mock_open = flexmock(sys.modules['__builtin__'])
     mock_open.should_call('open')
     (mock_open.should_receive('open')
      .with_args('newfile', 'wb')
      .and_return(
          flexmock(write=lambda x: None)
          .should_receive('write').with_args('READY').once().mock()
          .should_receive('flush').once().mock()
          .should_receive('close').once().mock()
          )
      )
     wr = FileWriter('newfile')
     c = Collector.start(self.controller, wr)
     c.tell(Record(None, 'READY'))
     ActorRegistry.stop_all()
Пример #10
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])
Пример #11
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])
Пример #12
0
def main():
    parser = argparse.ArgumentParser(
        description='Akka Spider')
    parser.add_argument('url',
                        help='URL seed, main url')

    parser.add_argument('-R', '--recursive-out-domain', action="store_true",
                        help='Continue recursion out of the domain')
    parser.add_argument('--ndownloaders', type=int, default=4, help='Number of downloaders')
    parser.add_argument('-w', '--write',  default='output.dot', help='Output file')
    args = parser.parse_args()

    if urlparse(args.url).scheme == '':
        raise Exception('Input url should contain scheme')
        return
    else:
        seed = [(args.url, args.url)]


    # Start actors
    queue = Queue.start().proxy()
    downloaders = map(
        lambda x: Downloader.start(queue).proxy()
        , range(args.ndownloaders))

    for url in seed:
        queue.push(url).get()

    if args.recursive_out_domain:
        exclusion_filter = lambda url: True
    else:
        exclusion_filter = lambda url: urlparse(url).netloc.endswith(urlparse(args.url).netloc)

    dot = scheduler(queue,
                    downloaders,
                    lambda: random.randrange(len(downloaders)),
                    exclusion_filter)

    with open(args.write, 'w') as f:
        f.write(dot.source)

    # Clean up
    ActorRegistry.stop_all()
Пример #13
0
    def test_fatalfailwork(self):
        class Worker(object):
            def __init__(self, arg):
                self.arg = arg

            def work_on(self, task):
                pass

        ctl = (flexmock(tell=lambda x: None)
               .should_receive('tell').once()
               .with_args(Resignition).mock())

        (flexmock(Worker, work_on=lambda x: None)
         .should_receive('work_on').once()
         .with_args(1).and_raise(ValueError()))

        cr = Crawler.start(ctl, self.tasksource, self.collector,
                           Worker, self.initargs)
        cr.tell(Task(None, 1))
        cr.stop()
        ActorRegistry.stop_all()
Пример #14
0
def test_stop_all_stops_last_started_actor_first_if_blocking(mocker):
    mocker.patch.object(ActorRegistry, 'get_all')

    stopped_actors = []
    started_actors = [mocker.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)

    assert stopped_actors[0] == started_actors[2]
    assert stopped_actors[1] == started_actors[1]
    assert stopped_actors[2] == started_actors[0]
Пример #15
0
def test_stop_all_stops_last_started_actor_first_if_blocking(mocker):
    mocker.patch.object(ActorRegistry, 'get_all')

    stopped_actors = []
    started_actors = [mocker.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)

    assert stopped_actors[0] == started_actors[2]
    assert stopped_actors[1] == started_actors[1]
    assert stopped_actors[2] == started_actors[0]
Пример #16
0
 def tearDownClass(cls):
   ActorRegistry.stop_all()
Пример #17
0
def test_all_actors_can_be_stopped_through_registry(a_actor_refs, b_actor_refs):
    assert len(ActorRegistry.get_all()) == 8

    ActorRegistry.stop_all(block=True)

    assert len(ActorRegistry.get_all()) == 0
Пример #18
0
 def stop(self):
   ActorRegistry.stop_all()
Пример #19
0
def test_actor_start_already_registered(actor_class):
    actor_ref = actor_class().start()
    assert len(ActorRegistry.get_all()) == 1
    assert actor_ref in ActorRegistry.get_all()
    ActorRegistry.stop_all()
Пример #20
0
 def tearDownClass(cls):
     ActorRegistry.stop_all()
Пример #21
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()))
Пример #22
0
 def tearDown(self):
     self.log_handler.close()
     ActorRegistry.stop_all()
Пример #23
0
def stop_all():
    yield
    ActorRegistry.stop_all()
Пример #24
0
    for _ in range(10000):
        actor.foo.get()


def test_direct_callable_attribute_access():
    actor = AnActor.start().proxy()
    for _ in range(10000):
        actor.func().get()


def test_traversable_plain_attribute_access():
    actor = AnActor.start().proxy()
    for _ in range(10000):
        actor.bar.cat.get()


def test_traversable_callable_attribute_access():
    actor = AnActor.start().proxy()
    for _ 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_traversable_plain_attribute_access)
        time_it(test_traversable_callable_attribute_access)
    finally:
        ActorRegistry.stop_all()
Пример #25
0
 def test_all_actors_can_be_stopped_through_registry(self):
     self.assertEqual(9, len(ActorRegistry.get_all()))
     ActorRegistry.stop_all(block=True)
     self.assertEqual(0, len(ActorRegistry.get_all()))
def teardown():
    ActorRegistry.stop_all()
Пример #27
0
def test_actor_has_an_uuid4_based_urn(actor_ref_B):
    actor_ref_custom = actor_ref_B
    assert uuid.UUID(actor_ref_custom.actor_urn).version == 4
    ActorRegistry.stop_all()
Пример #28
0
 def tearDown(self):
     self.log_handler.close()
     ActorRegistry.stop_all()
Пример #29
0
 def tearDown(self):
     ActorRegistry.stop_all()
Пример #30
0
 def tearDown(self):
     ActorRegistry.stop_all()
Пример #31
0
def stop_all():
    yield
    ActorRegistry.stop_all()
Пример #32
0
def test_actor_has_unique_urn(custom_actor_class):
    actor_refs = [custom_actor_class.start() for _ in range(3)]
    assert actor_refs[0].actor_urn != actor_refs[1].actor_urn
    assert actor_refs[1].actor_urn != actor_refs[2].actor_urn
    assert actor_refs[2].actor_urn != actor_refs[0].actor_urn
    ActorRegistry.stop_all()
Пример #33
0
 def stop(self):
     ActorRegistry.stop_all()
Пример #34
0
from pykka import ThreadingActor
from pykka import ActorRegistry

class Adder(ThreadingActor):
  def add_one(self, i):
    return i + 1

class Greeter(ThreadingActor):
  def on_receive(self, message):
    print(repr(message))
    print("Hi there!")

if __name__ == '__main__':
  actor_ref = Greeter.start()
  actor_ref.tell({'msg': 'herp'})
#  data = [1,2,3,4]
#  adder = Adder.start().proxy()
#  for datum in data:
#    print(adder.add_one(datum))
  _ = input("press anything to stop")
  ActorRegistry.stop_all()
Пример #35
0
def test_str_on_raw_actor_contains_actor_class_name(custom_actor_class):
    assert custom_actor_class().__class__.__name__ in str(custom_actor_class)
    ActorRegistry.stop_all()