def testBadConfig(self): cfg = Config() self.assertEqual(cfg.import_from_module('foo/bla/cnkjnckjcn.py'), []) cfg.set('config', None) self.assertEqual(cfg.config, None) cfg = Config(exclude=['config']) self.assertEqual(cfg.config, None)
def testSystem(self): from pulsar import system cfg = Config() self.assertEqual(cfg.uid, system.get_uid()) self.assertEqual(cfg.gid, system.get_gid()) self.assertEqual(cfg.proc_name, 'pulsar') cfg.set('process_name', 'bla') self.assertEqual(cfg.proc_name, 'bla')
def testFunctionFromConfigFile(self): worker = get_actor() cfg = Config() self.assertEqual(cfg.worker_task(worker), None) self.assertTrue(cfg.import_from_module(__file__)) self.assertEqual(cfg.worker_task(worker), worker) cfg1 = pickle.loads(pickle.dumps(cfg)) self.assertEqual(cfg1.worker_task(worker), worker)
def __testFunctionFromConfigFile(self): # TODO, fails in pypy for some odd reasons worker = get_actor() cfg = Config() self.assertEqual(cfg.connection_made(worker), None) self.assertTrue(cfg.import_from_module(__file__)) self.assertEqual(cfg.connection_made(worker), worker) cfg1 = pickle.loads(pickle.dumps(cfg)) self.assertEqual(cfg1.connection_made(worker), worker)
def testFunction(self): cfg = Config() worker = get_actor() self.assertTrue(cfg.post_fork) self.assertEqual(cfg.post_fork(worker), None) cfg.set('post_fork', post_fork) self.assertEqual(cfg.post_fork(worker), worker) cfg1 = pickle.loads(pickle.dumps(cfg)) self.assertEqual(cfg1.post_fork(worker), worker)
def testFunction(self): cfg = Config() worker = get_actor() self.assertTrue(cfg.arbiter_task) self.assertEqual(cfg.arbiter_task(worker), None) cfg.set('arbiter_task', worker_task) self.assertEqual(cfg.arbiter_task(worker), worker) cfg1 = pickle.loads(pickle.dumps(cfg)) self.assertEqual(cfg1.arbiter_task(worker), worker)
def _spawn_actor(cls, monitor, cfg=None, name=None, aid=None, **kw): # Internal function which spawns a new Actor and return its # ActorProxyMonitor. # *cls* is the Actor class # *monitor* can be either the ariber or a monitor kind = None if issubclass(cls, PoolMixin): kind = 'monitor' if monitor: params = monitor.actorparams() name = params.pop('name', name) aid = params.pop('aid', aid) cfg = params.pop('cfg', cfg) # get config if not available if cfg is None: if monitor: cfg = monitor.cfg.copy() else: cfg = Config() if not monitor: # monitor not available, this is the arbiter if kind != 'monitor': raise TypeError('class %s not a valid monitor' % cls) kind = 'arbiter' params = {} if not cfg.exc_id: if not aid: aid = gen_unique_id()[:8] cfg.set('exc_id', aid) # for key, value in iteritems(kw): if key in cfg.settings: cfg.set(key, value) else: params[key] = value # if monitor: if not kind: if not issubclass(cls, Actor): raise TypeError('Class %s not a valid actor.' % cls) kind = cfg.concurrency if not kind: raise TypeError('Cannot spawn class %s. not a valid concurrency.' % cls) actor_proxy = concurrency(kind, cls, monitor, cfg, name=name, aid=aid, **params) # Add to the list of managed actors if this is a remote actor if isinstance(actor_proxy, Actor): return actor_proxy else: actor_proxy.monitor = monitor monitor.managed_actors[actor_proxy.aid] = actor_proxy future = actor_proxy_future(actor_proxy) actor_proxy.start() return future
def _spawn_actor(kind, monitor, cfg=None, name=None, aid=None, **kw): # Internal function which spawns a new Actor and return its # ActorProxyMonitor. # *cls* is the Actor class # *monitor* can be either the arbiter or a monitor if monitor: params = monitor.actorparams() name = params.pop('name', name) aid = params.pop('aid', aid) cfg = params.pop('cfg', cfg) # get config if not available if cfg is None: if monitor: cfg = monitor.cfg.copy() else: cfg = Config() if not aid: aid = create_aid() if not monitor: # monitor not available, this is the arbiter assert kind == 'arbiter' name = kind params = {} if not cfg.exc_id: cfg.set('exc_id', aid) # for key, value in kw.items(): if key in cfg.settings: cfg.set(key, value) else: params[key] = value # if monitor: kind = kind or cfg.concurrency if not kind: raise TypeError('Cannot spawn') maker = concurrency_models.get(kind) if maker: c = maker() return c.make(kind, cfg, name, aid, monitor=monitor, **params) else: raise ValueError('Concurrency %s not supported in pulsar' % kind)
class FlaskGreen(MultiApp): """Multiapp Server configurator """ cfg = Config(bind=':8080', echo_bind=':8060') def build(self): yield self.new_app(WSGIServer, callable=FlaskSite()) yield self.new_app(SocketServer, 'echo', callable=EchoServerProtocol, echo_connection_made=log_connection)
def create_config(cls, params, prefix=None, name=None): """Create a new :class:`.Config` container. Invoked during initialisation, it overrides defaults with ``params`` and apply the ``prefix`` to non global settings. """ if isinstance(cls.cfg, Config): cfg = cls.cfg.copy(name=name, prefix=prefix) else: cfg = cls.cfg.copy() if name: cfg[name] = name if prefix: cfg[prefix] = prefix cfg = Config(**cfg) cfg.update_settings() cfg.update(params, True) return cfg
def __call__(self, actor=None): '''Register this application with the (optional) calling ``actor``. If an ``actor`` is available (either via the function argument or via the :func:`~pulsar.async.actor.get_actor` function) it must be :class:`.Arbiter`, otherwise this call is no-op. If no actor is available, it means this application starts pulsar engine by creating the :class:`.Arbiter` with its :ref:`global settings <setting-section-global-server-settings>` copied to the arbiter :class:`.Config` container. :return: the ``start`` one time event fired once this application has fired it. ''' if actor is None: actor = get_actor() monitor = None if actor and actor.is_arbiter(): monitor = actor.get_actor(self.name) if monitor is None and (not actor or actor.is_arbiter()): self.cfg.on_start() self.logger = self.cfg.configured_logger() if not actor: # arbiter not available # This application is starts the arbiter cfg = Config() cfg.update(self.arbiter_params()) actor = pulsar.arbiter(cfg=cfg) self.cfg.set('exc_id', actor.cfg.exc_id) if self.on_config(actor) is not False: start = Future(loop=actor._loop) actor.bind_event('start', partial(self._add_monitor, start)) return start else: return raise ImproperlyConfigured('Already started or not in arbiter domain')
def test_methods(self): cfg = Config() self.assertEqual(cfg.get('sdjcbsjkbcd', 'ciao'), 'ciao') d = dict(cfg.items()) self.assertEqual(len(d), len(cfg)) sett = cfg.get('debug') self.assertTrue(str(sett)) self.assertEqual(cfg.settings['debug'].default, False) cfg.set('debug', True, default=True) self.assertEqual(cfg.debug, True) self.assertEqual(cfg.settings['debug'].default, True)
request.actor.logger.info('Setting value') self.value = value def remote_get_value(self, request): request.actor.logger.info('Getting value') return self.value def start(arbiter, **kw): ensure_future(app(arbiter)) async def app(arbiter): # Spawn a new actor calc = await Calculator.spawn(name='calc1') print(calc.name) # set value in the remote calculator await calc.set_value(46) # get value from the remote calculator value = await calc.get_value() print(value) # Stop the application arbiter.stop() if __name__ == '__main__': cfg = Config() cfg.parse_command_line() arbiter(cfg=cfg, start=start).start()