Skip to main content

WorkForce Async

Asyncio Wrapper

Install in your project

$ pip install workforce-async

https://pypi.org/project/workforce-async/

Select your python version

$ poetry env use 3.7

Install Dependencies

$ poetry install

Run example

$ poetry run python examples/http_server.py

Run tests

$ poetry run pytest

Interfaces

Snippets taken from tests

Just run async functions

workforce = WorkForce()

async def foo():
    await asyncio.sleep(0.8)
    bar.count += 1

f1 = workforce.schedule(foo)

Just run normal functions in another thread

def foo():
    bar.count += 1

f = workforce.schedule(foo)

Function-based tasks

.s() supports both normal and async functions

workforce = WorkForce()

def callback(wf, task):
    bar.result = task.result()

@workforce.task(callback=callback)
async def add(a, b):
    return a + b

task = add.s(4, 5)()

@workforce.task()
async def sleep(sec):
    await asyncio.sleep(sec)

workforce.queue('channel1')
queue = sleep.q(0.5)('channel1')

Create queues of tasks

workforce = WorkForce()
queue = workforce.queue('channel1')
queue.put(foo())
queue.put(foo())
queue.put(foo())
assert len(queue) == 3

Class-based framework

Make your own workforce that distributes workitems to Workers

class Company(WorkForce):
    def get_worker(self, workitem):
        try:
            worker_name = {
                'NewFeature': 'developer',
                'Hire': 'hr',
                'EmployeeCounseling': 'hr'
            }[type(workitem).__name__]

            return super().get_worker(worker_name)
        except KeyError:
            raise self.WorkerNotFound

company = Company()

Make your own workers that perform tasks based on the workitem they receive

@company.worker
class Developer(Worker):
    def handle_workitem(self, workitem, *args, **kwargs):
        callback = getattr(workitem, 'callback', None)

        # All tasks here run concurrent
        coros = (getattr(self, task_name)(workitem)
                 for task_name in workitem.tasks)

        # Hack because asyncio.gather is not recognised as a coroutine
        async def gather(*aws, **kwargs):
            return await asyncio.gather(*aws, **kwargs)

        return gather(*coros), callback

    async def design(self, workitem):
        await asyncio.sleep(3)
        bar.arr.append('design')

    async def code(self, workitem):
        await asyncio.sleep(2)
        bar.arr.append('code')

    async def test(self, workitem):
        await asyncio.sleep(1)
        bar.arr.append('test')

    def make_pr(self, task, wf):
        time.sleep(0.2)
        bar.arr.append('make_pr')

company.schedule_workflow(NewFeature('New trendy ML'))

Metadata

Release files for workforce-async 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for workforce-async 0.12.0
File Size Uploaded Provenance Actions
workforce-async-0.12.0.tar.gz 6.6 kB Without provenance Details for workforce-async-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for workforce-async 0.12.0
File Interpreter ABI Platform Actions
workforce_async-0.12.0-py3-none-any.whl Python 3 none any Details for workforce_async-0.12.0-py3-none-any.whl

Total release size: 13.4 kB

Release files / workforce-async-0.12.0.tar.gz

Download URL workforce-async-0.12.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cc22d06b8bb309fad1bd2fa5e486b6d9a124f7c2a4cd3264f7bff932fab2920c
BLAKE2b-256 checksum
How to use checksums
cf6a2b1108042d3f8ec157f98cc5152783ab272eb67893277ba153ce0ff5ab00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Linux/5.15.19_1

Release files / workforce_async-0.12.0-py3-none-any.whl

Download URL workforce_async-0.12.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b17cc68505c76c7781117fc342ebd5840239caa6bd8eb9ee421a77a7839aaf63
BLAKE2b-256 checksum
How to use checksums
db62752bc62249b2184e27d5497b050c07aac291e444383eca2c37cf9f93a288
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Linux/5.15.19_1

Security for workforce-async 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page