Skip to main content

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

  1. Clone this repo
  2. Run poetry install
  3. Test using ./scripts/test
  4. Lint automatically using ./scripts/lint

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

aioworker-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

aioworker-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file aioworker-0.1.0.tar.gz.

File metadata

  • Download URL: aioworker-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.8.1 Darwin/17.7.0

File hashes

Hashes for aioworker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c75ecc5b0a2060fdea2d8d5bce5fc5e235b6c5e2f701f777c0850a013210d056
MD5 d8e9463a9c4720de588ea8eb753632b7
BLAKE2b-256 311d9406bc5388785e6926597a7b36a794b492840b338220e22c8d47fbc1e1ae

See more details on using hashes here.

File details

Details for the file aioworker-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aioworker-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.8.1 Darwin/17.7.0

File hashes

Hashes for aioworker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0917c27747c111c2abf29a2f3955a8d1cb412b4d7207f48b3912f68409930e0a
MD5 0d8acf2964366ba6c79b67b47ede2f2d
BLAKE2b-256 27e29b55c40583c7ce83bc6f9c023dce3fa83bc58c5ca1728ab524e1d5f7683d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page