Narration
Project description
narration
Logging is easy, but setup is messy and boring in multi processes contexts, due to the various way python can spawn processes (fork, spawn, forkserver) via explicit or implicit spawning context.
Narration handles the following logging setup boiler plate:
main process’s logger writes log record emitted by the process itself or its child processes
child process’s logger’s handler forward log records to the main process only
Installation
pip install narration
Usage
Create your loggers as you like (eg: logging.basicConfig(…))
On the master process, setup the logger to act as server:
import narration
import multiprocessing
import logging
ctx = mutiprocessing.get_context()
m = ctx.Manager()
logger=logging.getLogger()
client_handler_settings = narration.setup_server_handlers(logger=logger, ctx=ctx, ctx_manager=m)
logger.debug("Main process ready")
pool = multiprocessing.pool.Pool(10, child_process_worker_function, [client_handler_settings] , 2, ctx)
On the child process, setup the logger to act as ‘emitting’ client:
import narration
import multiprocessing
import logging
# Child process must receive the 'client_handler_settings' payload when starting the process
# Re-create a logger, replacing handlers/formatting (if any) with handler to forrward records to the master process
logger=logging.getLogger()
narration.setup_client_handlers(logger=logger, handler_name_to_client_handler_settings=client_handler_settings)
logger.debug("Child process ready")
Child process log records will be sent to the master process’s logger’s original handlers for procesing.
Main process ready
Child process ready
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file narration-0.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: narration-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 39.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86fdffec2b14bfc23b930c8939508d0f6b5be978975374fc7a53d5e50296ffc8 |
|
MD5 | fb26bd0329089ba102bdd51a48d85763 |
|
BLAKE2b-256 | cb0dea68ac911d6ed1e143d4a0eccd8b44dcd5061d48b96e88bdab4628b251da |