Application API¶
This module implements the main classes for pulsar application framework. The
framework is built on top of pulsar asynchronous engine and allows to
implement servers with very little effort. The main classes here are:
Application and MultiApp which, has the name suggests, is
a factory of several Application running on a single server.
The Configurator is a mixin used as base class for both
Application and MultiApp.
Configurator¶
-
class
pulsar.apps.Configurator(name=None, description=None, epilog=None, version=None, argv=None, parse_console=True, script=None, cfg=None, **params)[source]¶ A mixin for configuring and loading a pulsar application server.
Parameters: - name – to override the class
nameattribute. - description – to override the class
cfg.descriptionattribute. - epilog – to override the class
cfg.epilogattribute. - version – Optional version of this application, it overrides
the class
cfg.versionattribute. - argv – Optional list of command line parameters to parse, if
not supplied the
sys.argvlist will be used. The parameter is only relevant ifparse_consoleisTrue. - parse_console –
True(default) if the console parameters needs parsing. - script – Optional string which set the
scriptattribute. - params – a dictionary of configuration parameters which
overrides the defaults and the
cfgclass attribute. They will be overwritten by a config file or command line arguments.
-
name¶ The name is unique if this is an
Application. In this case it defines the application monitor name as well and can be access in the arbiter domain via theget_application()function.
-
argv¶ Optional list of command line parameters. If not available the
sys.argvlist will be used when parsing the console.
-
cfg¶ The
Configfor thisConfigurator. If set as class attribute it will be replaced during initialisation.Default:
None.
-
console_parsed¶ Trueif this application parsed the console before starting.
-
script¶ Full path of the script which starts the application or
None. Evaluated during initialization via thepython_path()method.
-
classmethod
create_config(params, prefix=None, name=None)[source]¶ Create a new
Configcontainer.Invoked during initialisation, it overrides defaults with
paramsand apply theprefixto non global settings.
-
load_config()[source]¶ Load the application configuration from a file and/or from the command line.
Called during application initialisation. The parameters overriding order is the following:
- default parameters.
- the key-valued params passed in the initialisation.
- the parameters in the optional configuration file
- the parameters passed in the command line.
-
on_config(arbiter)[source]¶ Callback when configuration is loaded.
This is a chance to do applications specific checks before the concurrent machinery is put into place.
If it returns
Falsethe application will abort.
-
python_path(script)[source]¶ Called during initialisation to obtain the
scriptname.If
scriptdoes not evaluate toTrueit is evaluated from the__main__import. Returns the real path of the python script which runs the application.
-
root_dir¶ Root directory of this
Configurator.Evaluated from the
scriptattribute.
-
start(exit=True)[source]¶ Invoked the application callable method and start the
arbiterif it wasn’t already started.It returns a
Futurecalled back once the application/applications are running. It returnsNoneif called more than once.
-
version¶ Version of this
Application
- name – to override the class
Application¶
-
class
pulsar.apps.Application(callable=None, load_config=True, **params)[source]¶ An application interface.
Applications can be of any sorts or forms and the library is shipped with several battery included examples in the
pulsar.appsmodule.These are the most important facts about a pulsar
Application:- It derives from
Configuratorso that it has all the functionalities to parse command line arguments and setup thecfgplaceholder ofSetting. - Instances of an
Applicationare callable objects accepting the calling actor as only argument. The callable method should not be overwritten, instead one should overwrites the application hooks available. - When an
Applicationis called for the first time, a newmonitoris added to thearbiter, ready to perform its duties.
Parameters: - callable – Initialise the
callableattribute. - load_config –
If
Falsetheload_config()method is not invoked.Default
True. - params – Passed to the
Configuratorinitialiser.
-
callable¶ Optional callable serving or configuring your application. If provided, the callable must be picklable, therefore it is either a function or a picklable object.
Default
None
-
__call__(actor=None)[source]¶ Register this application with the (optional) calling
actor.If an
actoris available (either via the function argument or via theget_actor()function) it must bearbiter, otherwise this call is no-op.If no actor is available, it means this application starts pulsar engine by creating the
arbiterwith its global settings copied to the arbiterConfigcontainer.Returns: the startone time event fired once this application has fired it.
-
actorparams(monitor, params=None)[source]¶ Hook to add additional entries when the monitor spawn new actors.
-
stream¶ Actor stream handler.
-
worker_start(worker, exc=None)[source]¶ Added to the
startworker hook.
-
worker_stopping(worker, exc=None)[source]¶ Added to the
stoppingworker hook.
- It derives from
Multi App¶
-
class
pulsar.apps.MultiApp(name=None, description=None, epilog=None, version=None, argv=None, parse_console=True, script=None, cfg=None, **params)[source]¶ A
MultiAppis a tool for creating severalApplicationand starting them at once.It makes sure all settings for the applications created are available in the command line.
MultiAppderives fromConfiguratorand therefore supports all its configuration utilities,build()is the only method which must be implemented by subclasses.A minimal example usage:
import pulsar class Server(pulsar.MultiApp): def build(self): yield self.new_app(TaskQueue) yield self.new_app(WSGIserver, "rpc", callable=..., ...) yield self.new_app(WSGIserver, "web", callable=..., ...)
-
apps()[source]¶ List of
Applicationfor thisMultiApp.The list is lazily loaded from the
build()method.
-
build()[source]¶ Virtual method, must be implemented by subclasses and return an iterable over results obtained from calls to the
new_app()method.
-
new_app(App, prefix=None, callable=None, **params)[source]¶ Invoke this method in the
build()method as many times as the number ofApplicationrequired by thisMultiApp.Parameters: - App – an
Applicationclass. - prefix – The prefix to use for the application,
the prefix is appended to
the application config parameters and to the
application name. Each call to this method must use a different
value of for this parameter. It can be
None. - callable – optional callable (function of object) used during
initialisation of App (the
Application.callable). - params – additional key-valued parameters used when creating an instance of App.
Returns: a tuple used by the
apps()method.- App – an
-
Get application¶
-
pulsar.apps.get_application(name)[source]¶ Fetch an
Applicationassociated withnameif available.This function may return an asynchronous component. The application name is set during initialisation. Check the
Configurator.nameattribute for more information.
When monitor start¶
The application framework provides a way for adding hooks which are executed every time a new application starts. A hook is registered by:
from pulsar.apps import when_monitor_start
def myhook(monitor):
...
when_monitor_start.append(myhook)
By default, the list of hooks only contains a callback to start the default data store if it needs to.