Small boilerplate with tools for concurrent/parallel processing.
Project description
minibone
minibone is an easy to use yet powerful boiler plate for multithreading, multiprocessing and others:
- Config: To handle configuration settings
- Daemon: To run a periodical task in another thread
- Emailer: To send emails in concurrent threads
- HTMLBase: To render html using snippets and toml configuration file in async mode
- HTTPt: HTTP client to do concurrent requests in threads
- Logging: To setup a logger friendly with filerotation
- IOThreads: To run concurrent tasks in threads
- PARProcesses: To run parallel CPU-bound tasks
- Storing: To queue and store files periodically in a thread (queue and forget)
It will be deployed to PyPi when a new release is created
Installation
pip install minibone
Config
Handle configuration settings in memory and/or persist them into toml/yaml/json formats
from minibone.config import Config
# Create a new set of settings and persist them
cfg = Config(settings={"listen": "localhost", "port": 80}, filepath="config.toml")
cfg.add("debug", True)
cfg.to_toml()
# Load settings from a file. Defaults can be set. More information: help(Config.from_toml)
cfg2 = Config.from_toml("config.toml")
# also there are async counter part methods
import asyncio
cfg3 = asyncio.run(Config.aiofrom_toml("config.toml"))
Usually config files are editted externaly then loaded as read only on your code, so in such case, you may want to subclass Config for easier usage
from minibone.config import Config
class MyConfig(Config):
def __init__(self):
defaults = {"main": {"listen": "localhost", "port": 80}}
settings = Config.from_toml(filepath="config.toml", defaults=defaults)
super().__init__(settings=settings)
@property
def listen(self) -> str:
return self["main"]["listen"]
@property
def port(self) -> int:
return self["main"]["port"]
if __name__ == "__main__":
cfg = MyConfig()
print(cfg.port)
# it will print the default port value if not port setting was defined in config.toml
Daemon
It is just another python class to run a periodical task in another thread. It can be used in two modes: subclasing and callback
Usage as SubClass mode
- Subclass Daemon
- call super().init() in yours
- Overwrite on_process method with yours
- Add logic you want to run inside on_process
- Be sure your methods are safe-thread to avoid race condition
- self.lock is available for lock.acquire / your_logic / lock.release
- call start() method to keep running on_process in a new thread
- call stop() to finish the thread
Check sample_clock.py for a sample
Usage as callback mode
- Instance Daemon by passing a callable
- Add logic to your callable method
- Be sure your callable is safe-thread to avoid race condition
- call start() method to keep running callable in a new thread
- call stop() to finish the thread
Check sample_clock_callback.py for a sample
AsyncDaemon
It is just another python class to run a periodical task using asyncio instead of threads. It can be used in two modes: subclasing and callback
Usage as SubClass mode
- Subclass AsyncDaemon
- call super().init() in yours
- Overwrite on_process method with yours (must be async)
- Add logic you want to run inside on_process
- Be sure your methods are async-safe to avoid race condition
- self.lock is available for async with self.lock context manager
- call await start() method to keep running on_process as a task
- call await stop() to finish the task
Check sample_async_clock.py for a sample
Usage as callback mode
- Instance AsyncDaemon by passing an async callable
- Add logic to your callable method (must be async)
- Be sure your callable is async-safe to avoid race condition
- call await start() method to keep running callable as a task
- call await stop() to finish the task
Check sample_async_clock_callback.py for a sample
Logging
Setup a logger using UTC time that outputs logs to stdin or to a file. It is friendly to filerotation (when setting output to a file)
import logging
from minibone.logging import setup_log
if __name__ == "__main__":
# setup_log must be called only once in your code.
# So you have to choice if logging to stdin or to a file when calling it
setup_log(level="INFO")
logging.info('This is a log to the stdin')
# or call the next lines instead if you want to log into a file
# setup_log(file="sample.log", level="INFO")
# logging.info('yay!')
Contribution
- Feel free to clone this repository and send any pull requests.
- Add issues if something is not working as expected.
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 Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file minibone-0.9.0.tar.gz.
File metadata
- Download URL: minibone-0.9.0.tar.gz
- Upload date:
- Size: 40.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b2b49041c3004af287014ccba091878bba297453f9c76ad0d8901ab1872953b
|
|
| MD5 |
1b93be98a976251819937c30ca4160f2
|
|
| BLAKE2b-256 |
f19bfa5b0e66dbaa17503b0827ad36c69a532eab8241d215e994d5238c576a28
|
File details
Details for the file minibone-0.9.0-py3-none-any.whl.
File metadata
- Download URL: minibone-0.9.0-py3-none-any.whl
- Upload date:
- Size: 38.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a620cfb787077fa4461dc3a568a26da7b81c1062bc1e9840d037052eb2099aa7
|
|
| MD5 |
3a825af5f451a800bbb64b297330e7ee
|
|
| BLAKE2b-256 |
77b4b281ed8729b85fc5b643c9448478e5bd5451562b6a8581e5534b2944c341
|