[![linting (flake8 & mypy)](https://github.com/medram/Pool_Workers/actions/workflows/lint.yml/badge.svg)](https://github.com/medram/Pool_Workers/actions/workflows/lint.yml) [![Tests & Coverage](https://github.com/medram/Pool_Workers/actions/workflows/tests.yml/badge.svg)](https://github.com/medram/Pool_Workers/actions/workflows/tests.yml)
# Pool_Workers
Pool_Workers is a small package for dealing with pools, workers and queues.
## Installation:
```bash
pip install pool-workers
```
## More info:
Usefull functions for Pool as well as Worker.
```python
"""
Default params:
Pool(max_workers=os.cpu_count() + 4, name=None, queue=None, wait_queue=True,
result_queue=None, workers_sleep=0.1, callback=None, execption_handler=execption_handler)
"""
from pool_workers import Pool
pool = Pool(...)
pool.start() # Start all workers to process queue tasks.
pool.is_alive() # Return true if is there any alive worker, else false.
pool.is_idle() # Return true if is there any worker in idle mode, else false.
pool.is_done() # Return true if the queue is empty (no tasks left to process).
pool.is_paused() # Return true if the all workers have been paused, else false.
pool.shutdown() # Abort all workers
pool.join() # Wait for all workers to finish the all queue tasks.
pool.result() # return a list result
pool.pause() # pause the all workers
pool.resume() # resume the all workers
pool.count() # count workers
pool.update() # adjust the number of workers
"""
default params:
Worker(name, queue, result=None, wait_queue=False, sleep=0.5, callback=None,
execption_handler=execption_handler)
"""
from pool_workers import Worker
worker = Worker(...)
worker.start()
worker.abort() # stop worker in a safe way
worker.aborted()
worker.pause()
worker.paused()
worker.resume()
# And like a normal thread, worker has also:
worker.is_alive()
worker.join()
```
## Usage
### Example 1:
```python
import time
import threading
import random
from queue import Queue
from pool_workers import Pool, Task
# Our logic to be performed Asynchronously.
def our_process(a):
t = threading.current_thread()
# just to semulate how mush time this logic is going to take to be done.
time.sleep(random.uniform(0, 3))
print(f'{t.getName()} is finished the task {a} ...')
# Our function to handle thrown exceptions from 'our_process' logic.
def execption_handler(thread_name, exception):
print(f'{thread_name}: {exception}')
# create a queue & pool.
q = Queue()
pool = Pool(name='Pool_1', queue=q, wait_queue=False, execption_handler=execption_handler)
# adding some tasks the the queue.
for i in range(10):
# Creating task with args and kwargs and push it into the queue.
task = Task(our_process, args=(i,), kwargs={})
q.put(task)
try:
# start the Pool
pool.start()
# go back to the main thread from time to another to check the KeyboardInterrupt
while pool.is_alive():
pool.join(0.5)
except (KeyboardInterrupt, SystemExit):
# shutdown the pool by aborting its Workers/threads.
pool.shutdown()
"""output result
Worker_1_Pool_1 is finished the task 1 ...
Worker_1_Pool_1 is finished the task 2 ...
Worker_0_Pool_1 is finished the task 0 ...
Worker_0_Pool_1 is finished the task 4 ...
Worker_0_Pool_1 is finished the task 5 ...
Worker_1_Pool_1 is finished the task 3 ...
Worker_0_Pool_1 is finished the task 6 ...
Worker_1_Pool_1 is finished the task 7 ...
Worker_0_Pool_1 is finished the task 8 ...
Worker_0_Pool_1: The Queue is empty.
Worker_1_Pool_1 is finished the task 9 ...
Worker_1_Pool_1: The Queue is empty.
Worker_0_Pool_1 is stopped
Worker_1_Pool_1 is stopped
Pool_1 is shutted down
"""
```
### Example 2:
```python
import time
import threading
import random
from queue import Queue
from pool_workers import Worker, Task
# Our logic to be performed Asynchronously.
def our_process(a):
t = threading.current_thread()
# just to semulate how mush time this logic is going to take to be done.
time.sleep(random.uniform(0, 3))
print(f'{t.getName()} is finished the task {a} ...')
# Our function to handle thrown exceptions from 'our_process' logic.
def execption_handler(thread_name, exception):
print(f'{thread_name}: {exception}')
# create a queue & pool.
q = Queue()
t = Worker(name='worker', queue=q, wait_queue=False, sleep=0.1, execption_handler=execption_handler)
# adding some tasks the the queue.
for i in range(10):
# Creating task with args and kwargs and push it into the queue.
task = Task(our_process, args=(i,), kwargs={})
q.put(task)
try:
# start the Pool
t.start()
# block the code execution here to check the KeyboardInterrupt (to stop the worker safely)
while t.is_alive():
t.join(0.5)
# Can't go here until the worker finishes his work.
except (KeyboardInterrupt, SystemExit):
# stop the Worker/thread.
t.abort()
"""output result
worker is finished the task 0 ...
worker is finished the task 1 ...
worker is finished the task 2 ...
worker is finished the task 3 ...
worker is finished the task 4 ...
worker is finished the task 5 ...
worker is finished the task 6 ...
worker is finished the task 7 ...
worker is finished the task 8 ...
worker is finished the task 9 ...
"""
```
## License
MIT License
Raw data
{
"_id": null,
"home_page": "https://github.com/medram/pool_workers",
"name": "pool-workers",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<4.0",
"maintainer_email": "",
"keywords": "threads,pools,threading,queue,pool-workers",
"author": "medram",
"author_email": "mramouchy@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/6c/65/a9e5fac6fc0c7456f198f83525d2f34f755aa688dafeba4ef55bd8d71981/pool_workers-0.0.6.tar.gz",
"platform": null,
"description": "\n[![linting (flake8 & mypy)](https://github.com/medram/Pool_Workers/actions/workflows/lint.yml/badge.svg)](https://github.com/medram/Pool_Workers/actions/workflows/lint.yml) [![Tests & Coverage](https://github.com/medram/Pool_Workers/actions/workflows/tests.yml/badge.svg)](https://github.com/medram/Pool_Workers/actions/workflows/tests.yml)\n\n# Pool_Workers\nPool_Workers is a small package for dealing with pools, workers and queues.\n\n\n## Installation:\n```bash\npip install pool-workers\n```\n## More info:\nUsefull functions for Pool as well as Worker.\n\n```python\n\"\"\"\nDefault params:\nPool(max_workers=os.cpu_count() + 4, name=None, queue=None, wait_queue=True,\n\tresult_queue=None, workers_sleep=0.1, callback=None, execption_handler=execption_handler)\n\"\"\"\nfrom pool_workers import Pool\n\npool = Pool(...)\n\npool.start()\t\t# Start all workers to process queue tasks.\npool.is_alive()\t\t# Return true if is there any alive worker, else false.\npool.is_idle()\t\t# Return true if is there any worker in idle mode, else false.\npool.is_done()\t\t# Return true if the queue is empty (no tasks left to process).\npool.is_paused()\t# Return true if the all workers have been paused, else false.\npool.shutdown()\t\t# Abort all workers\npool.join()\t\t\t# Wait for all workers to finish the all queue tasks.\npool.result()\t\t# return a list result\npool.pause()\t\t# pause the all workers\npool.resume()\t\t# resume the all workers\npool.count()\t\t# count workers\npool.update()\t\t# adjust the number of workers\n\n\"\"\"\ndefault params:\nWorker(name, queue, result=None, wait_queue=False, sleep=0.5, callback=None,\n\texecption_handler=execption_handler)\n\"\"\"\nfrom pool_workers import Worker\n\nworker = Worker(...)\n\nworker.start()\nworker.abort()\t\t# stop worker in a safe way\nworker.aborted()\nworker.pause()\nworker.paused()\nworker.resume()\n# And like a normal thread, worker has also:\nworker.is_alive()\nworker.join()\n\n\n```\n\n## Usage\n### Example 1:\n```python\nimport time\nimport threading\nimport random\n\nfrom queue import Queue\nfrom pool_workers import Pool, Task\n\n# Our logic to be performed Asynchronously.\ndef our_process(a):\n\tt = threading.current_thread()\n\t# just to semulate how mush time this logic is going to take to be done.\n\ttime.sleep(random.uniform(0, 3))\n\tprint(f'{t.getName()} is finished the task {a} ...')\n\n\n# Our function to handle thrown exceptions from 'our_process' logic.\ndef execption_handler(thread_name, exception):\n print(f'{thread_name}: {exception}')\n\n\n# create a queue & pool.\nq = Queue()\npool = Pool(name='Pool_1', queue=q, wait_queue=False, execption_handler=execption_handler)\n\n# adding some tasks the the queue.\nfor i in range(10):\n\t# Creating task with args and kwargs and push it into the queue.\n\ttask = Task(our_process, args=(i,), kwargs={})\n\tq.put(task)\n\ntry:\n\t# start the Pool\n\tpool.start()\n\t# go back to the main thread from time to another to check the KeyboardInterrupt\n\twhile pool.is_alive():\n\t\tpool.join(0.5)\n\nexcept (KeyboardInterrupt, SystemExit):\n\t# shutdown the pool by aborting its Workers/threads.\n\tpool.shutdown()\n\n\n\"\"\"output result\nWorker_1_Pool_1 is finished the task 1 ...\nWorker_1_Pool_1 is finished the task 2 ...\nWorker_0_Pool_1 is finished the task 0 ...\nWorker_0_Pool_1 is finished the task 4 ...\nWorker_0_Pool_1 is finished the task 5 ...\nWorker_1_Pool_1 is finished the task 3 ...\nWorker_0_Pool_1 is finished the task 6 ...\nWorker_1_Pool_1 is finished the task 7 ...\nWorker_0_Pool_1 is finished the task 8 ...\nWorker_0_Pool_1: The Queue is empty.\nWorker_1_Pool_1 is finished the task 9 ...\nWorker_1_Pool_1: The Queue is empty.\nWorker_0_Pool_1 is stopped\nWorker_1_Pool_1 is stopped\nPool_1 is shutted down\n\"\"\"\n```\n\n### Example 2:\n```python\nimport time\nimport threading\nimport random\n\nfrom queue import Queue\nfrom pool_workers import Worker, Task\n\n# Our logic to be performed Asynchronously.\ndef our_process(a):\n\tt = threading.current_thread()\n\t# just to semulate how mush time this logic is going to take to be done.\n\ttime.sleep(random.uniform(0, 3))\n\tprint(f'{t.getName()} is finished the task {a} ...')\n\n\n# Our function to handle thrown exceptions from 'our_process' logic.\ndef execption_handler(thread_name, exception):\n print(f'{thread_name}: {exception}')\n\n\n# create a queue & pool.\nq = Queue()\nt = Worker(name='worker', queue=q, wait_queue=False, sleep=0.1, execption_handler=execption_handler)\n\n# adding some tasks the the queue.\nfor i in range(10):\n\t# Creating task with args and kwargs and push it into the queue.\n\ttask = Task(our_process, args=(i,), kwargs={})\n\tq.put(task)\n\ntry:\n\t# start the Pool\n\tt.start()\n\t# block the code execution here to check the KeyboardInterrupt (to stop the worker safely)\n\twhile t.is_alive():\n\t\tt.join(0.5)\n\n\t# Can't go here until the worker finishes his work.\n\nexcept (KeyboardInterrupt, SystemExit):\n\t# stop the Worker/thread.\n\tt.abort()\n\n\"\"\"output result\nworker is finished the task 0 ...\nworker is finished the task 1 ...\nworker is finished the task 2 ...\nworker is finished the task 3 ...\nworker is finished the task 4 ...\nworker is finished the task 5 ...\nworker is finished the task 6 ...\nworker is finished the task 7 ...\nworker is finished the task 8 ...\nworker is finished the task 9 ...\n\"\"\"\n```\n\n## License\nMIT License\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "pool-workers is a light weight package for dealing & managing pools, workers and queues.",
"version": "0.0.6",
"project_urls": {
"Documentation": "https://github.com/medram/pool_workers",
"Homepage": "https://github.com/medram/pool_workers",
"Repository": "https://github.com/medram/pool_workers"
},
"split_keywords": [
"threads",
"pools",
"threading",
"queue",
"pool-workers"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c5fdf34c60ccbefd6a5b25499470573178ab1481520bbfd7743d0b0ff7d8195c",
"md5": "8ea4ee087ef6a296486f240176c5e60b",
"sha256": "26a0577031b37837ac2a770e32a9ebb9b2f04b3640eec9c3f3895ec1411af831"
},
"downloads": -1,
"filename": "pool_workers-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8ea4ee087ef6a296486f240176c5e60b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<4.0",
"size": 5421,
"upload_time": "2024-01-31T13:39:52",
"upload_time_iso_8601": "2024-01-31T13:39:52.420974Z",
"url": "https://files.pythonhosted.org/packages/c5/fd/f34c60ccbefd6a5b25499470573178ab1481520bbfd7743d0b0ff7d8195c/pool_workers-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6c65a9e5fac6fc0c7456f198f83525d2f34f755aa688dafeba4ef55bd8d71981",
"md5": "d22deaf6042532d16c7c04a21bb5a2e8",
"sha256": "1dc95385b999b7296094a02d075c30528180a807af1415a10e4c802762618bd1"
},
"downloads": -1,
"filename": "pool_workers-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "d22deaf6042532d16c7c04a21bb5a2e8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<4.0",
"size": 5039,
"upload_time": "2024-01-31T13:39:54",
"upload_time_iso_8601": "2024-01-31T13:39:54.164744Z",
"url": "https://files.pythonhosted.org/packages/6c/65/a9e5fac6fc0c7456f198f83525d2f34f755aa688dafeba4ef55bd8d71981/pool_workers-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-31 13:39:54",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "medram",
"github_project": "pool_workers",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "pool-workers"
}