Name | modernqueue JSON |
Version |
1.0.3
JSON |
| download |
home_page | |
Summary | A modern queue in a multithreaded environment |
upload_time | 2023-01-01 08:10:19 |
maintainer | |
docs_url | None |
author | Margot Louis |
requires_python | >=3.6 |
license | |
keywords |
thread
queue
modern
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# ModernQueue
A modern and permissive Python queue in a multithreaded environment.
Go to the [Wiki](https://github.com/PonyLucky/modern-queue/wiki) for more information.
## Table of contents
- [Installation](#installation)
- [From PyPI](#from-pypi)
- [From GitHub](#from-github)
- [Usage](#usage)
- [Importing](#importing)
- [Creating a queue](#creating-a-queue)
- [Adding functions to the queue](#adding-functions-to-the-queue)
- [Running the queue](#running-the-queue)
- [Getting the results](#getting-the-results)
- [Waiting for the queue to finish](#waiting-for-the-queue-to-finish)
- [Examples](#examples)
- [1)](#1)
- [2)](#2)
- [3)](#3)
- [License](#license)
## Installation
### From PyPI
```bash
pip install modernqueue
```
### From GitHub
Download `modernqueue.py` and put it in your project folder.
## Usage
### Importing
```python
from modernqueue import ModernQueue
```
### Creating a queue
```python
# Create a queue with 4 threads
queue = ModernQueue(max_threads=4)
# Create a queue with no limit on threads
queue = ModernQueue()
```
### Adding functions to the queue
```python
# Add a function to the queue
queue.add(func=print_number, args={'number': 1})
# Add a function to the queue, with tuple arguments
queue.add(func=print_number, args=(1,))
```
### Running the queue
```python
# Run the queue, blocking the function until finished
queue.run()
# Run the queue, without blocking the function
queue.run(is_blocking=False)
```
### Getting the results
```python
# Get the results of the queue, in order
results = queue.get_results()
# Get faster the results of the queue
results = queue.get_results(is_ordered=False)
```
### Waiting for the queue to finish
```python
# Wait for the queue to finish, in blocking mode
queue.run()
# Wait for the queue to finish, in non-blocking mode
queue.run(is_blocking=False)
while queue.running() != 0:
sleep(0.1)
```
## Examples
### 1)
Simple example of a queue with 4 threads, that prints numbers from 1 to 10, with a 1-second delay between each number.
This one is greatly commented, so you can understand how it works.
```python
# Define the function to run
def print_number(number: int) -> int:
"""
Print a number and sleep for 1 second.
Args:
- number (int): The number to print.
Returns:
- (int) The number multiplied by 2.
"""
sleep(1)
print(number)
return number * 2
# Create the queue, with a maximum of 4 threads
#
# max_threads is optional and defaults to -1 (no limit)
queue = ModernQueue(max_threads=4)
# Add the functions to the queue
for i in range(1, 11):
# There are 2 ways to pass arguments to the function
# 1. As a dict (kwargs):
queue.add(func=print_number, args={'number': i})
# 2. As a tuple (args):
# queue.add(func=print_number, args=(i,))
# Run the queue, blocking the function until finished
# is_blocking is optional and defaults to True
queue.run(is_blocking=True)
# Print "Done", if the function is blocking
# This will be printed after all the numbers are printed
print("Done")
# Get the results of the queue
#
# If you don't want to take the processing time to sort the results,
# set is_ordered to False
#
# is_ordered is optional and defaults to True
results = queue.get_results(is_ordered=True)
print(results)
# --- OUTPUT ---
# 4
# 2
# 3
# 1
# 5
# 7
# 6
# 8
# 9
# 10
# Done
# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```
### 2)
Same as above, but with fewer comments.
```python
# waiting for the queue to finish in blocking mode
queue = ModernQueue(max_threads=4)
for i in range(1, 11):
queue.add(func=print_number, args={'number': i})
queue.run(is_blocking=True)
print("Done")
results = queue.get_results(is_ordered=True)
print(results)
# --- OUTPUT ---
# 1
# 3
# 2
# 4
# 5
# 7
# 6
# 8
# 9
# 10
# Done
# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```
### 3)
Same as above, but in non-blocking mode.
It will print “Not done yet…” while the queue is running, and “Done” when it's finished. To mark when the process is finished and when it's not in the code.
```python
# waiting for the queue to finish in non-blocking mode
queue = ModernQueue(max_threads=4)
for i in range(1, 11):
queue.add(func=print_number, args={'number': i})
queue.run(is_blocking=False)
print("Not done yet...", f"({queue.running()} threads running)")
while queue.running() != 0:
sleep(0.1)
print("Done")
results = queue.get_results(is_ordered=True)
print(results)
# --- OUTPUT ---
# 1
# 3
# 2
# 4
# 5
# 7
# 6
# 8
# Not done yet... (2 threads running)
# 9
# 10
# Done
# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```
## License
You can use this code however you want, no credit is required.
Though, if you want to give me credit, you can do it by linking to my GitHub profile:
[https://github.com/PonyLucky](https://github.com/PonyLucky)
Raw data
{
"_id": null,
"home_page": "",
"name": "modernqueue",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "thread,queue,modern",
"author": "Margot Louis",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/1e/16/b27ee06b2e6c46863ce57f061b1b970e2d6ac090665460a59451de183c21/modernqueue-1.0.3.tar.gz",
"platform": null,
"description": "# ModernQueue\nA modern and permissive Python queue in a multithreaded environment.\n\nGo to the [Wiki](https://github.com/PonyLucky/modern-queue/wiki) for more information.\n\n## Table of contents\n- [Installation](#installation)\n - [From PyPI](#from-pypi)\n - [From GitHub](#from-github)\n- [Usage](#usage)\n - [Importing](#importing)\n - [Creating a queue](#creating-a-queue)\n - [Adding functions to the queue](#adding-functions-to-the-queue)\n - [Running the queue](#running-the-queue)\n - [Getting the results](#getting-the-results)\n - [Waiting for the queue to finish](#waiting-for-the-queue-to-finish)\n- [Examples](#examples)\n - [1)](#1)\n - [2)](#2)\n - [3)](#3)\n- [License](#license)\n\n## Installation\n### From PyPI\n```bash\npip install modernqueue\n```\n\n### From GitHub\nDownload `modernqueue.py` and put it in your project folder.\n\n## Usage\n### Importing\n```python\nfrom modernqueue import ModernQueue\n```\n\n### Creating a queue\n```python\n# Create a queue with 4 threads\nqueue = ModernQueue(max_threads=4)\n\n# Create a queue with no limit on threads\nqueue = ModernQueue()\n```\n\n### Adding functions to the queue\n```python\n# Add a function to the queue\nqueue.add(func=print_number, args={'number': 1})\n\n# Add a function to the queue, with tuple arguments\nqueue.add(func=print_number, args=(1,))\n```\n\n### Running the queue\n```python\n# Run the queue, blocking the function until finished\nqueue.run()\n\n# Run the queue, without blocking the function\nqueue.run(is_blocking=False)\n```\n\n### Getting the results\n```python\n# Get the results of the queue, in order\nresults = queue.get_results()\n\n# Get faster the results of the queue\nresults = queue.get_results(is_ordered=False)\n```\n\n### Waiting for the queue to finish\n```python\n# Wait for the queue to finish, in blocking mode\nqueue.run()\n\n# Wait for the queue to finish, in non-blocking mode\nqueue.run(is_blocking=False)\nwhile queue.running() != 0:\n sleep(0.1)\n```\n\n## Examples\n### 1)\nSimple example of a queue with 4 threads, that prints numbers from 1 to 10, with a 1-second delay between each number.\n\nThis one is greatly commented, so you can understand how it works.\n```python\n# Define the function to run\ndef print_number(number: int) -> int:\n \"\"\"\n Print a number and sleep for 1 second.\n\n Args:\n - number (int): The number to print.\n\n Returns:\n - (int) The number multiplied by 2.\n \"\"\"\n sleep(1)\n print(number)\n return number * 2\n\n# Create the queue, with a maximum of 4 threads\n#\n# max_threads is optional and defaults to -1 (no limit)\nqueue = ModernQueue(max_threads=4)\n\n# Add the functions to the queue\nfor i in range(1, 11):\n # There are 2 ways to pass arguments to the function\n # 1. As a dict (kwargs):\n queue.add(func=print_number, args={'number': i})\n # 2. As a tuple (args):\n # queue.add(func=print_number, args=(i,))\n\n# Run the queue, blocking the function until finished\n# is_blocking is optional and defaults to True\nqueue.run(is_blocking=True)\n\n# Print \"Done\", if the function is blocking\n# This will be printed after all the numbers are printed\nprint(\"Done\")\n\n# Get the results of the queue\n# \n# If you don't want to take the processing time to sort the results,\n# set is_ordered to False\n# \n# is_ordered is optional and defaults to True\nresults = queue.get_results(is_ordered=True)\nprint(results)\n\n# --- OUTPUT ---\n# 4\n# 2\n# 3\n# 1\n# 5\n# 7\n# 6\n# 8\n# 9\n# 10\n# Done\n# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n```\n\n### 2)\nSame as above, but with fewer comments.\n```python\n# waiting for the queue to finish in blocking mode\nqueue = ModernQueue(max_threads=4)\nfor i in range(1, 11):\n queue.add(func=print_number, args={'number': i})\nqueue.run(is_blocking=True)\nprint(\"Done\")\nresults = queue.get_results(is_ordered=True)\nprint(results)\n\n# --- OUTPUT ---\n# 1\n# 3\n# 2\n# 4\n# 5\n# 7\n# 6\n# 8\n# 9\n# 10\n# Done\n# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n```\n\n### 3)\nSame as above, but in non-blocking mode.\n\nIt will print \u201cNot done yet\u2026\u201d while the queue is running, and \u201cDone\u201d when it's finished. To mark when the process is finished and when it's not in the code.\n```python\n# waiting for the queue to finish in non-blocking mode\nqueue = ModernQueue(max_threads=4)\nfor i in range(1, 11):\n queue.add(func=print_number, args={'number': i})\nqueue.run(is_blocking=False)\nprint(\"Not done yet...\", f\"({queue.running()} threads running)\")\nwhile queue.running() != 0:\n sleep(0.1)\nprint(\"Done\")\nresults = queue.get_results(is_ordered=True)\nprint(results)\n\n# --- OUTPUT ---\n# 1\n# 3\n# 2\n# 4\n# 5\n# 7\n# 6\n# 8\n# Not done yet... (2 threads running)\n# 9\n# 10\n# Done\n# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n```\n\n## License\nYou can use this code however you want, no credit is required.\n\nThough, if you want to give me credit, you can do it by linking to my GitHub profile:\n[https://github.com/PonyLucky](https://github.com/PonyLucky)\n",
"bugtrack_url": null,
"license": "",
"summary": "A modern queue in a multithreaded environment",
"version": "1.0.3",
"split_keywords": [
"thread",
"queue",
"modern"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "763716c77d57f3cadd0aaeaa3df38599",
"sha256": "abd04f7dedf82f7bfda1fa65bbdd7549cde1aa267f358e9f009a16fdcb955591"
},
"downloads": -1,
"filename": "modernqueue-1.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "763716c77d57f3cadd0aaeaa3df38599",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 4345,
"upload_time": "2023-01-01T08:10:17",
"upload_time_iso_8601": "2023-01-01T08:10:17.855924Z",
"url": "https://files.pythonhosted.org/packages/a2/81/8054c3f47016c939ddf2aa4eec83f64205b6aebef3aee6856f29cd07b210/modernqueue-1.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "8391c348cfdbfc62e2982b5c7a1eba4b",
"sha256": "84b576e7fe6bb92a103bb77a210694e04a0acbcf5e029e579b7e07f01663aaed"
},
"downloads": -1,
"filename": "modernqueue-1.0.3.tar.gz",
"has_sig": false,
"md5_digest": "8391c348cfdbfc62e2982b5c7a1eba4b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 4231,
"upload_time": "2023-01-01T08:10:19",
"upload_time_iso_8601": "2023-01-01T08:10:19.687919Z",
"url": "https://files.pythonhosted.org/packages/1e/16/b27ee06b2e6c46863ce57f061b1b970e2d6ac090665460a59451de183c21/modernqueue-1.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-01 08:10:19",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "modernqueue"
}