faust.worker

Worker.

A “worker” starts a single instance of a Faust application.

See also

Starting the App: for more information.

class faust.worker.Worker(app: faust.types.app.AppT, *services, sensors: Iterable[faust.types.sensors.SensorT] = None, debug: bool = False, quiet: bool = False, loglevel: Union[str, int] = None, logfile: Union[str, IO] = None, stdout: IO = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, stderr: IO = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, blocking_timeout: float = 10.0, workdir: Union[pathlib.Path, str] = None, console_port: int = 50101, loop: asyncio.events.AbstractEventLoop = None, redirect_stdouts: bool = None, redirect_stdouts_level: int = None, **kwargs) → None[source]

Worker.

Usage:

You can start a worker using:

  1. the faust worker program.

  2. instantiating Worker programmatically and calling execute_from_commandline():

    >>> worker = Worker(app)
    >>> worker.execute_from_commandline()
    
  3. or if you already have an event loop, calling await start, but in that case you are responsible for gracefully shutting down the event loop:

    async def start_worker(worker: Worker) -> None:
        await worker.start()
    
    def manage_loop():
        loop = asyncio.get_event_loop()
        worker = Worker(app, loop=loop)
        try:
            loop.run_until_complete(start_worker(worker)
        finally:
            worker.stop_and_shutdown_loop()
    
Parameters:
  • app (AppT[]) – The Faust app to start.
  • *services – Services to start with worker. This includes application instances to start.
  • sensors (Iterable[SensorT]) – List of sensors to include.
  • debug (bool) – Enables debugging mode [disabled by default].
  • quiet (bool) – Do not output anything to console [disabled by default].
  • loglevel (Union[str, int]) – Level to use for logging, can be string (one of: CRIT|ERROR|WARN|INFO|DEBUG), or integer.
  • logfile (Union[str, IO]) – Name of file or a stream to log to.
  • stdout (IO) – Standard out stream.
  • stderr (IO) – Standard err stream.
  • blocking_timeout (float) – When debug is enabled this sets the timeout for detecting that the event loop is blocked.
  • workdir (Union[str, Path]) – Custom working directory for the process that the worker will change into when started. This working directory change is permanent for the process, or until something else changes the working directory again.
  • loop (asyncio.AbstractEventLoop) – Custom event loop object.
logger = <Logger faust.worker (WARNING)>
app = None

The Faust app started by this worker.

sensors = None

Additional sensors to add to the Faust app.

workdir = None

Current working directory. Note that if passed as an argument to Worker, the worker will change to this directory when started.

spinner = None

Class that displays a terminal progress spinner (see progress).

on_init_dependencies() → Iterable[mode.types.services.ServiceT][source]

Callback to be used to add service dependencies.

Return type:Iterable[ServiceT[]]
change_workdir(path: pathlib.Path) → None[source]
Return type:None
autodiscover() → None[source]
Return type:None
coroutine on_execute(self) → None[source]
Return type:None
coroutine on_first_start(self) → None[source]

Called only the first time the service is started.

Return type:None
coroutine on_startup_finished(self) → None[source]
Return type:None
on_worker_shutdown() → None[source]
Return type:None
on_setup_root_logger(logger: logging.Logger, level: int) → None[source]
Return type:None