Skip to main content

更简单的使用异步协程和线程池

Project description

newpool

更简单的使用异步协程和线程池

安装

pip install newpool

使用

异步协程

newpool.async_gather() 并发执行多个协程, 返回结果列表, 遇到异常不会中断, 而是将错误原因返回到结果列表中 newpool.async_taskgroup() 并发执行多个协程, 返回列表, 包含更详细的状态, 遇到异常会中断

import newpool

async def test():
    await newpool.async_sleep(1)
    print("test")

gather_results = newpool.async_gather([test() for i in range(20)], sem=5) # sem 为并发协程数
taskgroup_results = newpool.async_taskgroup([test() for i in range(20)]) # 此处没有使用 sem, 默认基于设备自动计算并发协程数

线程池

基于 concurrent.futures.ThreadPoolExecutor 实现

import newpool
import time

pool = newpool.Pool(5)

@pool.task
def test():
    time.sleep(2)
    print("test")

for i in range(20):
    test()

pool.wait() # 等待所有任务执行完毕
pool.state() # 线程池状态
pool.results # 所有任务执行结果
pool.cancel_all() # 取消所有任务

内置可以直接使用的线程池 newpool.pool, 无需实例化。 线程数默认基于设备自动计算, 也可以通过 newpool.pool.n = 5 修改线程数

import newpool

@newpool.pool.task
def test():
    print("test")

for i in range(20):
    test()

newpool.pool.wait() # 等待所有任务执行完毕
newpool.pool.state() # 线程池状态
newpool.pool.results # 所有任务执行结果
newpool.pool.cancel_all() # 取消所有任务

基于 asyncio 实现的线程池

使用 newpool.async_gather() 或者 newpool.async_taskgroup(), 通过 asyncio.run 运行。

默认线程数基于设备自动计算,也可以通过 newpool.to_async(n=5) 修改线程数。

import newpool
import time

@newpool.to_async()
def test():
    time.sleep(2)
    print("test")

tasks = newpool.async_gather([test() for i in range(20)])
newpool.asyncio_run(tasks)

@newpool.to_async(n=5)
def test2():
    time.sleep(2)
    print("test")

tasks2 = newpool.async_gather([test2() for i in range(20)])
newpool.asyncio_run(tasks2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

newpool-0.2.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

newpool-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file newpool-0.2.0.tar.gz.

File metadata

  • Download URL: newpool-0.2.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.26.1 CPython/3.12.10 Windows/11

File hashes

Hashes for newpool-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7b5daacd86fd50c548587d50c48268ad6f83efd7e44ca78393384285e0793055
MD5 a660b97f4439c2f14482d109a40fcbd2
BLAKE2b-256 7291c813ffa1c17b2b0b5ee9843d11c1d550a36c775d6bcc370bc6de3e6c9498

See more details on using hashes here.

File details

Details for the file newpool-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: newpool-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.26.1 CPython/3.12.10 Windows/11

File hashes

Hashes for newpool-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c52980880fea5af63b1aaf53d4986786674275d76571173e40c1538951a1d0cc
MD5 4a4f633345d9b683a153aefa5b86299b
BLAKE2b-256 99a5e292277fe8c27fd6d91d871fa09852fa637f01b34ccdad5c6fb84e201e52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page