Python async worker
Project description
aioworker
A Python worker running over asyncio
Requirements
python 3.7+
Usage
import asyncio
from aioworker import Service, Worker
async def task_1(loop):
while True:
print('Hello world')
await asyncio.sleep(2)
if __name__ == '__main__':
# Run the server using 1 worker processes.
Service(Worker(
tasks=[task_1],
)).run(num_workers=1)
or run tasks and the webserver
import asyncio
from aioworker import Service, Worker
async def sleeping(loop):
while True:
print('Sleeping for 2 seconds...')
await asyncio.sleep(2)
async def on_client_connect(reader, writer):
"""
Read up tp 300 bytes of TCP. This could be parsed usign the HTTP protocol for example
"""
data = await reader.read(300)
print(f'TCP Server data received: {data} \n')
writer.write(data)
await writer.drain()
writer.close()
if __name__ == '__main__':
# Run the server using 1 worker processes.
Service(Worker(
tasks=[sleeping],
web_server_config={
'client_connected_cb': on_client_connect,
},
)).run(num_workers=1)
How to stop the worker
ctrl+c
Default values
Variable | Default |
---|---|
TCP server host | 0.0.0.0 |
TPC server port | 8888 |
Development
- Clone this repo
- Run
poetry install
- Test using
./scripts/test
- Lint automatically using
./scripts/lint
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
aioworker-0.1.0.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for aioworker-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0917c27747c111c2abf29a2f3955a8d1cb412b4d7207f48b3912f68409930e0a |
|
MD5 | 0d8acf2964366ba6c79b67b47ede2f2d |
|
BLAKE2b-256 | 27e29b55c40583c7ce83bc6f9c023dce3fa83bc58c5ca1728ab524e1d5f7683d |