Name | aiothreading JSON |
Version |
0.1.3
JSON |
| download |
home_page | None |
Summary | AsyncIO version of the standard threading module |
upload_time | 2025-02-12 15:35:55 |
maintainer | None |
docs_url | None |
author | Vizonex |
requires_python | None |
license | None |
keywords |
aiothreading
threading
asyncio
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# AioThreading
A Fork of aiomultiprocess built around threading for enhancing performance whenever processes can't be used
[](https://badge.fury.io/py/aiothreading)
[](https://badge.fury.io/py/aiothreading)
[](https://opensource.org/licenses/MIT)
[](https://github.com/ambv/black)
__After 6 years of waiting, it finally exists.__ This library was inspired by the amazing work of Amyreese and the other contributors of [aiomultiprocess](https://github.com/omnilib/aiomultiprocess) as well as the original ThreadPoolExecutor apart of the python standard library, originally my plan was to write my own threadpool that could do this and many different versions were attempted by me over the past 2 years which all of them had problems of their own such as workers failing to exit propperly and more. Eventlually I had decided to fork this branch off instead with the goal of implementing this feature that I have long wanted which was asyncio but with threads. There are some occasions where this can be useful such as running a discord bot and http server at the same time when you only have so many computer cpu cores to work with. This can come in handy when you do not need to spawn or fork in a process but at the same time you need to reduce the main loop's overall workload which can be helpful if your goal is to send multiple http requests for instance as sometimes some things can block that you didn't indend or expect to be blocked that shouldn't, aiothreading attemps solve these problems and more of them. The best part of all, no more having to ask on sites like stackoverflow about how to run a thread with an asyncio eventloop, I have just solved that problem.
## Installation
```
pip install aiothreading
```
## Examples
### Threads
```python
from aiothreading import Thread, Worker
from aiohttp import request
async def do_request(link:str) -> str:
async with request("GET", link) as resp:
# get data before closing
data = await resp.json()
print(f"Your IP address is: {data['origin']}")
return data['origin']
async def main():
thread = Thread(target=do_request, args="https://httpbin.org/ip")
thread.start()
# to join the thread you can either await it or join it
await thread.join()
# This will also work...
await thread
async def worker():
# Wokers have the ability to return items after execution completes...
# NOTE: Workers will be renamed to ThreadWorker in a future update so aiomutliprocess doesn't collide with this class object
worker = Worker(target=do_request, args="https://httpbin.org/ip")
worker.start()
await worker.join()
ip = worker.result
print(ip)
```
## Integrating with Uvloop or Winloop
A Faster eventloop such as __uvloop__ or __winloop__ have been programmed to have better performance than python's stdlib asyncio
The ThreadPool and Threads and Workers can be used to make your threads run a bit faster.
```python
# Import uvloop if your not on windows
import platform
if platform.platform == "Windows":
import winloop as uvloop
else:
import uvloop
from aiothreading import Thread, Worker, ThreadPool
await Thread(
target=some_coro, loop_initializer=uvloop.new_event_loop
)
result = await Worker(
target=other_coro, loop_initializer=uvloop.new_event_loop
)
async with ThreadPool(loop_initalizer=uvloop.new_event_loop) as pool:
...
```
## ThreadPool
__ThreadPool__ is a renamed version of `aiomultiprocess.Pool` so that if you were to import and use both of them at the same time for whatever reasons the names don't overlap
```python
# Incase you need to use both of them at the same time
from aiomultiprocess import Pool
from aiothreading import ThreadPool
```
This example was taken from aiomultiprocess
```python
from asyncio import gather
from aiothreading import ThreadPool
async def get(url):
async with request("GET", url) as response:
return await response.text("utf-8")
# the threadpool works the same as aiomultiprocess.Pool
async with ThreadPool() as pool:
a, b, c, d = gather(
pool.apply(get, "https://github.com"),
pool.apply(get, "https://noswap.com"),
pool.apply(get, "https://omnilib.dev"),
pool.apply(get, "https://github.com/Vizonex")
)
```
## When to Use Aiothreading Over AioMultiprocessing
- High task consumption over a single loop on a lower-end device
- Networking or loading takes longer than usual in cases such as webscraping over the Tor Network
- Your writing an application via excutable such as compiling with pyinstaller which is Notorious
for not working with multiprocessing but you don't want something that will wind up acting too slow
- When you need to run something such as a discord bot and http server over the same process
examples of when you would do this would be when running a health check on the discord bot.
- When you don't need something fancy or grandiose.
- Your working with a potato operating system, rasberrypi, libre-computer, etc...
- When you need to combine aiomultiprocess library with this one.
- Low CPU overall
If none of these fit your criteria I highly encourage you to use the aiomultiprocessing library, it's way faster and has smarter exiting techniques.
## TODOS
- [ ] add aiologic to fix responsiveness of some threads to fix speeds and performance.
- [ ] Youtube Video about how to use
Raw data
{
"_id": null,
"home_page": null,
"name": "aiothreading",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "aiothreading, threading, asyncio",
"author": "Vizonex",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/bd/01/197af7ebc24404a36d61d7af06de5351dc453ef8fc796186a88d22fc296a/aiothreading-0.1.3.tar.gz",
"platform": null,
"description": "# AioThreading\n\nA Fork of aiomultiprocess built around threading for enhancing performance whenever processes can't be used\n\n\n[](https://badge.fury.io/py/aiothreading)\n[](https://badge.fury.io/py/aiothreading)\n[](https://opensource.org/licenses/MIT)\n[](https://github.com/ambv/black)\n\n\n__After 6 years of waiting, it finally exists.__ This library was inspired by the amazing work of Amyreese and the other contributors of [aiomultiprocess](https://github.com/omnilib/aiomultiprocess) as well as the original ThreadPoolExecutor apart of the python standard library, originally my plan was to write my own threadpool that could do this and many different versions were attempted by me over the past 2 years which all of them had problems of their own such as workers failing to exit propperly and more. Eventlually I had decided to fork this branch off instead with the goal of implementing this feature that I have long wanted which was asyncio but with threads. There are some occasions where this can be useful such as running a discord bot and http server at the same time when you only have so many computer cpu cores to work with. This can come in handy when you do not need to spawn or fork in a process but at the same time you need to reduce the main loop's overall workload which can be helpful if your goal is to send multiple http requests for instance as sometimes some things can block that you didn't indend or expect to be blocked that shouldn't, aiothreading attemps solve these problems and more of them. The best part of all, no more having to ask on sites like stackoverflow about how to run a thread with an asyncio eventloop, I have just solved that problem.\n\n\n\n## Installation\n\n``` \npip install aiothreading\n```\n\n\n\n## Examples \n\n### Threads\n```python\nfrom aiothreading import Thread, Worker\nfrom aiohttp import request\n\n\nasync def do_request(link:str) -> str:\n async with request(\"GET\", link) as resp:\n # get data before closing\n data = await resp.json()\n print(f\"Your IP address is: {data['origin']}\") \n return data['origin']\n\nasync def main():\n thread = Thread(target=do_request, args=\"https://httpbin.org/ip\")\n thread.start()\n # to join the thread you can either await it or join it \n await thread.join()\n\n # This will also work...\n await thread\n\n\n\nasync def worker():\n # Wokers have the ability to return items after execution completes...\n # NOTE: Workers will be renamed to ThreadWorker in a future update so aiomutliprocess doesn't collide with this class object\n worker = Worker(target=do_request, args=\"https://httpbin.org/ip\")\n worker.start()\n\n await worker.join()\n ip = worker.result\n print(ip)\n\n```\n\n## Integrating with Uvloop or Winloop\n\nA Faster eventloop such as __uvloop__ or __winloop__ have been programmed to have better performance than python's stdlib asyncio \nThe ThreadPool and Threads and Workers can be used to make your threads run a bit faster.\n\n```python\n# Import uvloop if your not on windows\nimport platform \nif platform.platform == \"Windows\":\n import winloop as uvloop\nelse:\n import uvloop\n\nfrom aiothreading import Thread, Worker, ThreadPool\n\nawait Thread(\n target=some_coro, loop_initializer=uvloop.new_event_loop\n)\nresult = await Worker(\n target=other_coro, loop_initializer=uvloop.new_event_loop\n)\n\nasync with ThreadPool(loop_initalizer=uvloop.new_event_loop) as pool:\n ...\n\n```\n\n\n\n## ThreadPool\n__ThreadPool__ is a renamed version of `aiomultiprocess.Pool` so that if you were to import and use both of them at the same time for whatever reasons the names don't overlap\n\n```python\n# Incase you need to use both of them at the same time\nfrom aiomultiprocess import Pool\nfrom aiothreading import ThreadPool\n```\n\nThis example was taken from aiomultiprocess\n\n```python\nfrom asyncio import gather\nfrom aiothreading import ThreadPool\n\nasync def get(url):\n async with request(\"GET\", url) as response:\n return await response.text(\"utf-8\")\n\n# the threadpool works the same as aiomultiprocess.Pool \nasync with ThreadPool() as pool:\n a, b, c, d = gather(\n pool.apply(get, \"https://github.com\"),\n pool.apply(get, \"https://noswap.com\"),\n pool.apply(get, \"https://omnilib.dev\"),\n pool.apply(get, \"https://github.com/Vizonex\")\n )\n\n```\n\n\n\n## When to Use Aiothreading Over AioMultiprocessing\n\n- High task consumption over a single loop on a lower-end device\n\n- Networking or loading takes longer than usual in cases such as webscraping over the Tor Network\n\n- Your writing an application via excutable such as compiling with pyinstaller which is Notorious\nfor not working with multiprocessing but you don't want something that will wind up acting too slow\n\n- When you need to run something such as a discord bot and http server over the same process\nexamples of when you would do this would be when running a health check on the discord bot.\n\n- When you don't need something fancy or grandiose.\n\n- Your working with a potato operating system, rasberrypi, libre-computer, etc...\n\n- When you need to combine aiomultiprocess library with this one. \n\n- Low CPU overall\n\n\n\nIf none of these fit your criteria I highly encourage you to use the aiomultiprocessing library, it's way faster and has smarter exiting techniques.\n\n## TODOS\n- [ ] add aiologic to fix responsiveness of some threads to fix speeds and performance.\n- [ ] Youtube Video about how to use\n\n",
"bugtrack_url": null,
"license": null,
"summary": "AsyncIO version of the standard threading module",
"version": "0.1.3",
"project_urls": null,
"split_keywords": [
"aiothreading",
" threading",
" asyncio"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "7a3127f793a60e450279d4440f5b81d4a67f0de9af9b3f610a56b60d9faa3d83",
"md5": "76019a8d9202913e55c313807ffbb06c",
"sha256": "b33d7dcd8fe7dee818d704fdd3f7e774f295fefdc70416e1c71c8304925e0496"
},
"downloads": -1,
"filename": "aiothreading-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "76019a8d9202913e55c313807ffbb06c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 21898,
"upload_time": "2025-02-12T15:35:51",
"upload_time_iso_8601": "2025-02-12T15:35:51.675180Z",
"url": "https://files.pythonhosted.org/packages/7a/31/27f793a60e450279d4440f5b81d4a67f0de9af9b3f610a56b60d9faa3d83/aiothreading-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "bd01197af7ebc24404a36d61d7af06de5351dc453ef8fc796186a88d22fc296a",
"md5": "0b2ad0e1b2bd18a7cae764c60df067e2",
"sha256": "4a07ddbe1c3eb8746ee85621dba1ed2eaa861900244069776c4c2034c28ae2f8"
},
"downloads": -1,
"filename": "aiothreading-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "0b2ad0e1b2bd18a7cae764c60df067e2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 20588,
"upload_time": "2025-02-12T15:35:55",
"upload_time_iso_8601": "2025-02-12T15:35:55.213906Z",
"url": "https://files.pythonhosted.org/packages/bd/01/197af7ebc24404a36d61d7af06de5351dc453ef8fc796186a88d22fc296a/aiothreading-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-12 15:35:55",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "aiothreading"
}