Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

coworker

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

coworker

Generic worker that performs concurrent tasks using coroutine.

  • 2.0.1
  • PyPI
  • Socket score

Maintainers
1

coworker

Generic worker that performs concurrent tasks using coroutine.

Quick Start Tutorial

Define how a task is performed -- do_task takes a single input and optionally returns a result:

.. code-block:: python

from coworker import Coworker

class SquareWorker(Coworker):
    async def do_task(self, task):  # Task can be anything, such as a tuple with a callable and args.
        return task * task

Now let's do some work:

.. code-block:: python

import asyncio

async def background_worker_example():
    worker = SquareWorker(max_concurrency=5)    # Only 5 tasks will run concurrently. Defalts to 10

    # Single task
    result = await worker.do(2)
    print(result)   # result = 4

    # Mulitiple tasks -- get all results after everything is done
    tasks = list(range(100))
    results = await worker.do(tasks)
    print(results)  # results = [0, 1, 4, 9, ...]

    # Mulitiple tasks -- get first completed
    tasks = list(range(10))
    for result in worker.do(tasks, as_iterator=True):
        print(await result)  # results = 0, 1, 4, 9, ...

# Run async usage example
asyncio.run(background_worker_example())

Besides max_concurrency, you can also pass the following params to Coworker:

  • sliding_window to use sliding window or tumbling window when processing the tasks concurrently. Defaults to True.
  • do_task for a callable to do the task instead of having to sub-class. E.g. Coworker(do_task=lamdba x: x * x)

| PyPI Package: https://pypi.python.org/pypi/coworker | GitHub Source: https://github.com/maxzheng/coworker | Report Issues/Bugs: https://github.com/maxzheng/coworker/issues | | Connect: https://www.linkedin.com/in/maxzheng | Contact: maxzheng.os @t gmail.com

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc