coworker


Namecoworker JSON
Version 1.0.4 PyPI version JSON
download
home_pagehttps://github.com/maxzheng/coworker
SummaryGeneric worker that performs concurrent tasks using coroutine.
upload_time2023-09-27 02:03:47
maintainer
docs_urlNone
authorMax Zheng
requires_python
licenseMIT
keywords coroutine concurrent worker
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            coworker
==============

Generic worker that performs concurrent tasks using coroutine.

Quick Start Tutorial
====================

Define how a task is performed -- 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

To run in the background forever and add tasks:

.. code-block:: python

    import asyncio

    async def background_worker_example():
        worker = SquareWorker(max_concurrency=5,    # Only 5 tasks will run concurrently
                              auto_start=True)      # Automatically start worker in the background after adding tasks
                                                    # and exits too when done.

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

        # Mulitiple tasks -- get first completed
        tasks = list(range(10))
        for task in asyncio.as_completed(worker.add_tasks(tasks)):
            print(await task)  # results = 0, 1, 4, 9, ...

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

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

To run for a list of tasks and stop worker when finished:

.. code-block:: python

    task_futures = asyncio.run(SquareWorker().start([1, 2, 3]))
    print([t.result() for t in task_futures])   # [1, 4, 9]


Links & Contact Info
====================

| Documentation: http://coworker.readthedocs.org
|
| 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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/maxzheng/coworker",
    "name": "coworker",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "coroutine concurrent worker",
    "author": "Max Zheng",
    "author_email": "maxzheng.os @t gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0b/6c/19229e0c4008ccfc684a020a2516dd5a9c3d07d0f7373786ef0b20ea5466/coworker-1.0.4.tar.gz",
    "platform": null,
    "description": "coworker\n==============\n\nGeneric worker that performs concurrent tasks using coroutine.\n\nQuick Start Tutorial\n====================\n\nDefine how a task is performed -- takes a single input and optionally returns a result:\n\n.. code-block:: python\n\n    from coworker import Coworker\n\n    class SquareWorker(Coworker):\n        async def do_task(self, task):  # Task can be anything, such as a tuple with a callable and args.\n            return task * task\n\nTo run in the background forever and add tasks:\n\n.. code-block:: python\n\n    import asyncio\n\n    async def background_worker_example():\n        worker = SquareWorker(max_concurrency=5,    # Only 5 tasks will run concurrently\n                              auto_start=True)      # Automatically start worker in the background after adding tasks\n                                                    # and exits too when done.\n\n        # Mulitiple tasks -- get all results after everything is done\n        tasks = list(range(100))\n        results = await asyncio.gather(*worker.add_tasks(tasks))\n        print(results)  # results = [0, 1, 4, 9, ...]\n\n        # Mulitiple tasks -- get first completed\n        tasks = list(range(10))\n        for task in asyncio.as_completed(worker.add_tasks(tasks)):\n            print(await task)  # results = 0, 1, 4, 9, ...\n\n        # Single task\n        result = await worker.add_tasks(2)\n        print(result)   # result = 4\n\n    # Run async usage example\n    asyncio.run(background_worker_example())\n\nTo run for a list of tasks and stop worker when finished:\n\n.. code-block:: python\n\n    task_futures = asyncio.run(SquareWorker().start([1, 2, 3]))\n    print([t.result() for t in task_futures])   # [1, 4, 9]\n\n\nLinks & Contact Info\n====================\n\n| Documentation: http://coworker.readthedocs.org\n|\n| PyPI Package: https://pypi.python.org/pypi/coworker\n| GitHub Source: https://github.com/maxzheng/coworker\n| Report Issues/Bugs: https://github.com/maxzheng/coworker/issues\n|\n| Connect: https://www.linkedin.com/in/maxzheng\n| Contact: maxzheng.os @t gmail.com\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Generic worker that performs concurrent tasks using coroutine.",
    "version": "1.0.4",
    "project_urls": {
        "Homepage": "https://github.com/maxzheng/coworker"
    },
    "split_keywords": [
        "coroutine",
        "concurrent",
        "worker"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ef5a818ca55c99ffee7933102b6de0866c1b12de7dbfc3706501921da1757fb5",
                "md5": "3eadcf42bff230f04272dbc9d28fa5be",
                "sha256": "2658d3766141e434c7f7369cbe7a76174b629d92799a4479a24bfa9b825321ff"
            },
            "downloads": -1,
            "filename": "coworker-1.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3eadcf42bff230f04272dbc9d28fa5be",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5523,
            "upload_time": "2023-09-27T02:03:45",
            "upload_time_iso_8601": "2023-09-27T02:03:45.952504Z",
            "url": "https://files.pythonhosted.org/packages/ef/5a/818ca55c99ffee7933102b6de0866c1b12de7dbfc3706501921da1757fb5/coworker-1.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0b6c19229e0c4008ccfc684a020a2516dd5a9c3d07d0f7373786ef0b20ea5466",
                "md5": "61fbe9f92b137c8e41fd35b516a8d40f",
                "sha256": "14fd160aa56c4beee25fe5418ae81660586e956957a678caeeef445ae5fbeb9c"
            },
            "downloads": -1,
            "filename": "coworker-1.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "61fbe9f92b137c8e41fd35b516a8d40f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14069,
            "upload_time": "2023-09-27T02:03:47",
            "upload_time_iso_8601": "2023-09-27T02:03:47.651477Z",
            "url": "https://files.pythonhosted.org/packages/0b/6c/19229e0c4008ccfc684a020a2516dd5a9c3d07d0f7373786ef0b20ea5466/coworker-1.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-27 02:03:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "maxzheng",
    "github_project": "coworker",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "coworker"
}
        
Elapsed time: 0.13155s