Skip to main content

简便的多进程调用工具,基于 concurrent.futures.ProcessPoolExecutor 的封装

Project description

Util Process Pool

简便的多进程调用工具,基于 concurrent.futures.ProcessPoolExecutor 的封装。

特性

  • 🚀 简化的多进程调用接口
  • 📊 进度跟踪和错误处理
  • 🔧 灵活的配置选项
  • 📝 详细的日志记录
  • 🎯 支持多种任务类型(CPU密集型、IO密集型)

安装

pip install util-process-pool

快速开始

基本使用

from util_process_pool import parallel_map

# 简单的并行计算
numbers = list(range(10))
results = parallel_map(lambda x: x * x, numbers)
print(results)  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

使用 ProcessPool 类

from util_process_pool import ProcessPool

def process_data(data):
    # 处理数据的函数
    return data * 2

with ProcessPool(max_workers=4) as pool:
    data_list = [1, 2, 3, 4, 5]
    results = list(pool.map(process_data, data_list))
    print(results)  # [2, 4, 6, 8, 10]

批量执行复杂任务

from util_process_pool import batch_execute

def complex_task(param1, param2):
    # 复杂的处理逻辑
    return f"Processed: {param1} + {param2}"

# 准备参数列表
args_list = [
    (("hello", "world"),),  # 第一个任务
    (("foo", "bar"),),      # 第二个任务
]

results = batch_execute(complex_task, args_list, show_progress=True)

API 文档

ProcessPool 类

主要的进程池管理类。

ProcessPool(
    max_workers=None,      # 最大工作进程数(默认:CPU核心数)
    initializer=None,      # 进程初始化函数
    initargs=(),           # 初始化函数参数
    logger=None            # 自定义日志记录器
)

方法:

  • submit(func, *args, **kwargs): 提交单个任务
  • map(func, iterable, timeout=None, chunksize=1): 并行映射
  • execute_batch(func, args_list, timeout=None, show_progress=False): 批量执行
  • wait_completion(timeout=None): 等待所有任务完成

工具函数

  • parallel_map(func, iterable, max_workers=None, chunksize=1, timeout=None)
  • batch_execute(func, args_list, max_workers=None, timeout=None, show_progress=False)

许可证

MIT License

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

util_process_pool-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

util_process_pool-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file util_process_pool-0.1.0.tar.gz.

File metadata

  • Download URL: util_process_pool-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.7

File hashes

Hashes for util_process_pool-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2a297550db0d85e4a7e51e68ec91c8e30cd34c82521e7f5b5b718a52af5fe52f
MD5 84097d176feb02cee1fba015a18c1957
BLAKE2b-256 d1ce0f888d24034ee7a87c81541254e4d88864c6545d01baf553e079ad5f9f54

See more details on using hashes here.

File details

Details for the file util_process_pool-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for util_process_pool-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd9f25826a95a8819b494f88078dbce5d97df513fe7990bac7186c23a21c8955
MD5 e0a7dd947b65c48e667de373d9116081
BLAKE2b-256 b6a95e0e6ed66240a3286c68e8592f0f283c727de7fc2e7b488c73b182ed32c6

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