def __call__(self, a=None): if a is None: a = yield spawn(name='greeter') if names: name = names.pop() send(a, 'greetme', {'name': name}) self._loop.call_later(1, self, a) else: arbiter().stop()
def spawn_actor_from_actor(actor, name): actor2 = yield from spawn(name=name) pong = yield from send(actor2, 'ping') assert pong == 'pong', 'no pong from actor' t1 = time() # cover the notify from a fron actor t2 = yield from send(actor2, 'notify', {}) assert t2 >= t1 return actor2.aid
def spawn(self, concurrency=None, **kwargs): concurrency = concurrency or self.concurrency ad = pulsar.spawn(concurrency=concurrency, **kwargs) self.assertTrue(ad.aid) self.assertTrue(isinstance(ad, pulsar.ActorProxyDeferred)) yield ad self.a = ad.result self.all_spawned.append(self.a) self.assertEqual(self.a.aid, ad.aid) self.assertTrue(self.a.address)
def __call__(self, a=None): if a is None: a = yield from pulsar.spawn(name='greeter') if names: name = names.pop() self._loop.logger.info("Hi! I'm %s" % name) yield from pulsar.send(a, 'greetme', {'name': name}) self._loop.call_later(1, pulsar. async, self(a)) else: pulsar.arbiter().stop()
def __call__(self, a=None): if a is None: a = yield from pulsar.spawn(name='greeter') if names: name = names.pop() self._loop.logger.info("Hi! I'm %s" % name) yield from pulsar.send(a, 'greetme', {'name': name}) self._loop.call_later(1, pulsar.async, self(a)) else: pulsar.arbiter().stop()
def test_spawning_in_arbiter(self): arbiter = pulsar.get_actor() self.assertEqual(arbiter.name, 'arbiter') self.assertTrue(len(arbiter.monitors) >= 1) name = 'testSpawning-%s' % self.concurrency future = spawn(name=name, concurrency=self.concurrency) self.assertTrue(future.aid in arbiter.managed_actors) proxy = yield future self.assertEqual(future.aid, proxy.aid) self.assertEqual(proxy.name, name) self.assertTrue(proxy.aid in arbiter.managed_actors) yield send(proxy, 'stop')
def spawn_actor(self, concurrency=None, **kwargs): '''Spawn a new actor and perform some tests.''' concurrency = concurrency or self.concurrency ad = pulsar.spawn(concurrency=concurrency, **kwargs) self.assertTrue(ad.aid) self.assertTrue(isinstance(ad, ActorProxyFuture)) proxy = yield ad self.all_spawned.append(proxy) self.assertEqual(proxy.aid, ad.aid) self.assertEqual(proxy.proxy, proxy) self.assertTrue(proxy.cfg) coroutine_return(proxy)
def test_spawning_in_arbiter(self): arbiter = pulsar.get_actor() self.assertEqual(arbiter.name, 'arbiter') self.assertTrue(len(arbiter.monitors) >= 1) name = 'testSpawning-%s' % self.concurrency future = spawn(name=name, concurrency=self.concurrency) self.assertTrue(future.aid in arbiter.managed_actors) yield future proxy = future.result self.assertEqual(future.aid, proxy.aid) self.assertEqual(proxy.name, name) self.assertTrue(proxy.aid in arbiter.managed_actors) yield send(proxy, 'stop')
def spawn_actor(self, concurrency=None, **kwargs): '''Spawn a new actor and perform some tests ''' concurrency = concurrency or self.concurrency ad = pulsar.spawn(concurrency=concurrency, **kwargs) self.assertTrue(ad.aid) self.assertTrue(isinstance(ad, ActorProxyFuture)) proxy = yield ad self.all_spawned.append(proxy) self.assertEqual(proxy.aid, ad.aid) self.assertEqual(proxy.proxy, proxy) self.assertTrue(proxy.cfg) coroutine_return(proxy)
async def spawn_actor(self, concurrency=None, **kwargs): '''Spawn a new actor and perform some tests ''' concurrency = concurrency or self.concurrency ad = pulsar.spawn(concurrency=concurrency, **kwargs) self.assertTrue(ad.aid) self.assertIsInstance(ad, asyncio.Future) proxy = await ad self.all_spawned.append(proxy) self.assertEqual(proxy.aid, ad.aid) self.assertEqual(proxy.proxy, proxy) self.assertTrue(proxy.cfg) return proxy
def spawn(self, concurrency=None, **kwargs): '''Spawn a new actor and perform some tests.''' concurrency = concurrency or self.concurrency ad = pulsar.spawn(concurrency=concurrency, **kwargs) self.assertTrue(ad.aid) self.assertTrue(isinstance(ad, pulsar.ActorProxyDeferred)) yield ad proxy = ad.result self.all_spawned.append(proxy) self.assertEqual(proxy.aid, ad.aid) self.assertEqual(proxy.proxy, proxy) self.assertTrue(proxy.cfg) yield proxy
def spawn_actor(self, concurrency=None, **kwargs): '''Spawn a new actor and perform some tests ''' concurrency = concurrency or self.concurrency ad = pulsar.spawn(concurrency=concurrency, **kwargs) self.assertTrue(ad.aid) self.assertIsInstance(ad, asyncio.Future) proxy = yield from ad self.all_spawned.append(proxy) self.assertEqual(proxy.aid, ad.aid) self.assertEqual(proxy.proxy, proxy) self.assertTrue(proxy.cfg) return proxy
def testSpawning(self): arbiter = pulsar.get_actor() self.assertEqual(arbiter.name, 'arbiter') self.assertTrue(len(arbiter.monitors) >= 1) future = spawn(name='testSpawning', concurrency=self.concurrency) self.assertTrue(future.aid in arbiter.spawning_actors) self.assertFalse(future.aid in arbiter.managed_actors) yield future proxy = future.result self.assertEqual(future.aid, proxy.aid) self.assertFalse(future.aid in arbiter.spawning_actors) self.assertEqual(proxy.name, 'testSpawning') self.assertTrue(proxy.aid in arbiter.managed_actors) yield send(proxy, 'stop')
def app(arbiter): # Spawn a new actor proxy = yield from spawn(name='actor1') print(proxy.name) # Execute inner method in actor1 result = yield from send(proxy, 'run', inner_method) print(result) yield from send(proxy, 'run', set_value, 10) value = yield from send(proxy, 'run', get_value) print(value) # Stop the application arbiter.stop()
def test_spawning_in_arbiter(self): arbiter = pulsar.get_actor() self.assertEqual(arbiter.name, 'arbiter') self.assertTrue(len(arbiter.monitors) >= 1) name = 'testSpawning-%s' % self.concurrency future = spawn(name=name, concurrency=self.concurrency) self.assertTrue(future.aid in arbiter.managed_actors) proxy = yield future self.assertEqual(future.aid, proxy.aid) self.assertEqual(proxy.name, name) self.assertTrue(proxy.aid in arbiter.managed_actors) self.assertEqual(proxy, arbiter.get_actor(proxy.aid)) # # Stop the actor result = yield send(proxy, 'stop') self.assertEqual(result, None) # result = yield wait_for_stop(self, proxy.aid) self.assertEqual(result, None)
def test_spawning_in_arbiter(self): arbiter = pulsar.get_actor() self.assertEqual(arbiter.name, 'arbiter') self.assertTrue(len(arbiter.monitors) >= 1) name = 'testSpawning-%s' % self.concurrency future = spawn(name=name, concurrency=self.concurrency) self.assertTrue(future.aid in arbiter.managed_actors) proxy = yield from future self.assertEqual(future.aid, proxy.aid) self.assertEqual(proxy.name, name) self.assertTrue(proxy.aid in arbiter.managed_actors) self.assertEqual(proxy, arbiter.get_actor(proxy.aid)) # yield from asyncio.sleep(1) # Stop the actor result = yield from send(proxy, 'stop') self.assertEqual(result, None) # result = yield from wait_for_stop(self, proxy.aid) self.assertEqual(result, None)
from functools import partial from pulsar import spawn, TcpServer import pulsar def create_echo_server(address, actor): '''Starts an echo server on a newly spawn actor''' server = TcpServer(actor.event_loop, address[0], address[1], EchoServerProtocol) yield server.start_serving() actor.servers['echo'] = server actor.extra['echo-address'] = server.address arbiter = pulsar.arbiter() proxy = spawn(start=partial(create_echo_server, 'localhost:9898'))
def test_bad_concurrency(self): # bla concurrency does not exists actor = yield pulsar.spawn(kind='bla') self.assertFalse(actor)
def async(fn): def task_wrapper(actor, *args, **kwargs): return fn(*args, **kwargs) fn.async = lambda *k, **kw: pulsar.spawn(start=partial(task_wrapper, *k, **kw)) return fn
def period_task(fn): def task_wrapper(actor): return fn() if pulsar.get_actor(): pulsar.spawn(period_task=task_wrapper) return fn
# -*- coding: utf-8 -*- from pulsar import spawn import pulsar import random class PeriodicTask: def __call__(self, actor): actor.event_loop.call_repeatedly(2, self.task) def task(self): print(random.randint(0, 9)) arbiter = pulsar.arbiter() ap = spawn(start=PeriodicTask())
def spawn(cls, **kwargs): proxy = yield from spawn(**kwargs) return cls(proxy)
def actor1(request, message): act2 = yield from spawn(name='actor2') r = send(act2, 'actor2', dict(message="Important data")) r.add_done_callback(lambda resolved_promise: print(resolved_promise)) return "Message sent"
def test_bad_concurrency(self): # bla concurrency does not exists actor = yield pulsar.spawn(kind="bla") self.assertFalse(actor)
def __call__(self, a=None): act1 = yield from spawn(name='actor1') r = send(act1, 'actor1', dict(data=[1, 2, 4])) r.add_done_callback( lambda resolved_promise: print(resolved_promise.result())) arbiter().stop()