Boilerplate for asyncio service
Project description
Boilerplate for asyncio service
This module provides boilerplate for microservices written in asyncio:
- Runner with task reference handler and graceful shutdown
- Configuration provider
- Logger with color support
import asyncio
from aiobp import runner
async def main():
try:
await asyncio.sleep(60)
except asyncio.CancelledError:
print('Saving data...')
runner(main())
More complex example
import asyncio
import aiohttp # just for example
import sys
from aiobp import create_task, on_shutdown, runner
from aiobp.config import InvalidConfigFile, sys_argv_or_filenames
from aiobp.config.conf import loader
from aiobp.logging import LoggingConfig, add_devel_log_level, log, setup_logging
class WorkerConfig:
"""Your microservice worker configuration"""
sleep: int = 5
class Config:
"""Put configurations together"""
worker: WorkerConfig
log: LoggingConfig
async def worker(config: WorkerConfig, client_session: aiohttp.ClientSession) -> int:
"""Perform service work"""
attempts = 0
try:
async with client_session.get('http://python.org') as resp:
assert resp.status == 200
log.debug('Page length %d', len(await resp.text()))
attempts += 1
await asyncio.sleep(config.sleep)
except asyncio.CancelledError:
log.info('Doing some shutdown work')
await client_session.post('http://localhost/service/attempts', data={'attempts': attempts})
return attempts
async def service(config: Config):
"""Your microservice"""
client_session = aiohttp.ClientSession()
on_shutdown(client_session.close, after_tasks_cancel=True)
create_task(worker(config.worker, client_session), 'PythonFetcher')
# you can do some monitoring, statistics collection, etc.
# or just let the method finish and the runner will wait for Ctrl+C or kill
def main():
"""Example microservice"""
add_devel_log_level()
try:
config_filename = sys_argv_or_filenames('service.local.conf', 'service.conf')
config = loader(Config, config_filename)
except InvalidConfigFile as error:
print(f'Invalid configuration: {error}')
sys.exit(1)
setup_logging(config.log)
log.info("Using config file: %s", config_filename)
runner(service(config))
if __name__ == '__main__':
main()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiobp-0.3.0.tar.gz
(9.8 kB
view details)
Built Distribution
aiobp-0.3.0-py3-none-any.whl
(11.9 kB
view details)
File details
Details for the file aiobp-0.3.0.tar.gz
.
File metadata
- Download URL: aiobp-0.3.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.8.7-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0a02c27172eb57fd968565a5b7c9d5a28f53d822b4ca00d298263da73eb468e |
|
MD5 | e9a399da583705f3b3ece9705b12f309 |
|
BLAKE2b-256 | 2e52a4b9e3bd22eb28c0c663c4c98c48ad0b4d4c02bbb4f57b3b028d838e9349 |
File details
Details for the file aiobp-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: aiobp-0.3.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.8.7-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df4218a1e2a1e4eec9f365a101d3b64241710f40a4eb0428c7b673b6a0b14391 |
|
MD5 | 90a87c7de8d88b0dcbd559df6e9859d4 |
|
BLAKE2b-256 | e442faccd135e64cdac4705fa47dc3c359abdb9928ea0c240924533b4e5ea539 |