Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberbolt committed Apr 30, 2023
1 parent d0da1ec commit 6a63748
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 9 deletions.
138 changes: 137 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,137 @@
# multiaio
# aiopools

[中文版](#中文版)

AIOPools is a Python AsyncIO Semaphore wrapper for building AsyncIO coroutine pools. ThreadPoolExecutor (thread pool) and ProcessPoolExecutor (process pool) similar to concurrent.futures, designed to enable Python code to achieve the highest number of concurrency.

## installation method

Environmental requirements: Python 3.8 and above

1. Enter the command window, create a virtual environment, and enter the following commands in sequence

Linux and macOS:

```bash
python3 -m venv venv # Create a virtual environment
.venv/bin/activate # Activate the virtual environment
```

Windows:

```bash
python -m venv venv # Create a virtual environment
venv\Scripts\activate # activate the virtual environment
```

2. Install AIOPools, enter in sequence


```python
pip install --upgrade pip
pip install aiopools
```

## Link

- GitHub: [https://github.com/Cyberbolt/aiopools](https://github.com/Cyberbolt/aiopools)
- Cyber Light Notes: [https://www.cyberlight.xyz/](https://www.cyberlight.xyz/)

## Minimal example

```python
'''
The total number of coroutine tasks in this example is 100. If the maximum allowed number of coroutines maxio is set to 20, it needs to run 5 times to complete.
'''

import asyncio
from aiopools import AsyncPool


async def block_io(i):
await asyncio. sleep(1)
print(i)
return i


async def main():
# Set the maximum number of coroutines allowed maxio
async with AsyncPool(maxio=20) as pool:
tasks = [pool. create_task(block_io(i)) for i in range(100)]

response = await asyncio. gather(*tasks)
print(response)


if __name__ == '__main__':
asyncio. run(main())

```

# 中文版

AIOPools 是 Python AsyncIO Semaphore 的包装器,用于构建 AsyncIO 协程池。类似于 concurrent.futures 的 ThreadPoolExecutor (线程池) 和 ProcessPoolExecutor (进程池),旨在使 Python 代码达到最高并发数。

## 安装方法

环境要求:Python 3.8 及以上

1.进入命令窗口,创建虚拟环境,依次输入以下命令

Linux 和 macOS:

```bash
python3 -m venv venv # 创建虚拟环境
. venv/bin/activate # 激活虚拟环境
```

Windows:

```bash
python -m venv venv # 创建虚拟环境
venv\Scripts\activate # 激活虚拟环境
```

2.安装 AIOPools,依次输入


```python
pip install --upgrade pip
pip install aiopools
```

## 链接

- GitHub: [https://github.com/Cyberbolt/aiopools](https://github.com/Cyberbolt/aiopools)
- 电光笔记: [https://www.cyberlight.xyz/](https://www.cyberlight.xyz/)

## 最简示例

```python
'''
该示例的协程任务数总计 100 个,设置最大允许的协程数 maxio 为 20,则需要运行 5 次才能完成。
'''

import asyncio
from aiopools import AsyncPool


async def block_io(i):
await asyncio.sleep(1)
print(i)
return i


async def main():
# 设置最大允许的协程数 maxio
async with AsyncPool(maxio=20) as pool:
tasks = [pool.create_task(block_io(i)) for i in range(100)]

response = await asyncio.gather(*tasks)
print(response)


if __name__ == '__main__':
asyncio.run(main())

```
22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import setuptools


with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="AIOPools",
version="0.1.0",
author="Cyberbolt",
author_email="dtconlyone@gmail.com",
description="AIOPools is a lightweight library for building Python AsyncIO coroutine pools. | AIOPools 是一个轻量级的用于构建 Python AsyncIO 协程池的库。",
long_description=long_description,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
install_requires=[]
)
21 changes: 13 additions & 8 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
'''
The total number of coroutine tasks in this example is 100. If the maximum allowed number of coroutines maxio is set to 20, it needs to run 5 times to complete.
'''

import asyncio
from aiopools import AsyncPool


async def block_io(i):
await asyncio.sleep(1)
print(i)
return i
await asyncio. sleep(1)
print(i)
return i


async def main():
async with AsyncPool(maxio=20) as pool:
tasks = [pool.create_task(block_io(i)) for i in range(100)]
# Set the maximum number of coroutines allowed maxio
async with AsyncPool(maxio=20) as pool:
tasks = [pool. create_task(block_io(i)) for i in range(100)]

response = await asyncio.gather(*tasks)
print(response)
response = await asyncio. gather(*tasks)
print(response)


if __name__ == '__main__':
asyncio.run(main())
asyncio. run(main())

0 comments on commit 6a63748

Please sign in to comment.