Parallel Python(pp) 包的使用总结

安装方式:

下载页面中 选择合适的版本进行安装, 如 zip后缀的文件,解压后,  进入文件目录中, 通过  python setup.py install 进行安装;

 

功能特点:

  • SMP和集群上并行执行python代码; 说白了 就是 一个机器上利用CPU多核,或者 在多个机器上利用每个机器的CPU 执行python代码;
  • 易于理解和实现基于Job的并行化技术(易于并行转换串行应用程序)
  • 自动检测并使用 CPU上 所有进程数作为 worker数; 默认就是此设置;
  • 运行时 worker数量可以进行修改;
  • 具有相同功能的后续作业的开销很低(实现透明缓存以减少开销)
  • 动态负载平衡(作业在运行时在处理器之间分配)
  • 容错性(如果其中一个节点失败,则将任务重新安排到其他节点上)
  • 计算资源 可以自动发现;
  • 自动发现计算资源 和容错  导致了 动态分配计算资源;
  • 基于SHA认证的网络连接;
  • 跨平台可移植性和互操作性(Windows,Linux,Unix,Mac OS X)
  • 跨架构可移植性和互操作性(x86,x86-64等)
  • 完全开源;

 

官方文档 的示例代码 有很多问题,不易理解;如下为 摸索后的示例代码;

server端 多线程+PP  集群服务 通过密码认证 的示例代码:

import time
from concurrent.futures._base import as_completed
from concurrent.futures.thread import ThreadPoolExecutor

import pp

# 分布式计算集群中 worker服务的 IP:PORT
ppservers = ('192.168.2.3:35000',)
# 启动pp master 服务
job_server = pp.Server(ppservers=ppservers, secret='mys')


def sum_primes(n):
    """
    导入的包 必须在里面
    :param n:
    :return:
    """
    # 可以在函数内加入 如下一行,这时 job_server.submit方法第四个参数便不再需要
    import time
    time.sleep(n)
    print(n, 'sleep')
    return n


def use(n):
    job = job_server.submit(sum_primes, (n,))
    return job()


print("Starting pp with", job_server.get_ncpus(), "workers")
pool = ThreadPoolExecutor()

pool_result = [pool.submit(use, func_param) for func_param in [1, 3, 4, 2, 5]]
for result in as_completed(pool_result):
    job_server.print_stats()
    item = result.result()
    if int(item) > 3:
        break
    print(item, 'df')
print('finish')

pp.Server对象初始化时 参数含义及用法:

  • ppservers: 参数值必须为 tuple类型, 每个元素格式为  IP地址:端口号
  • secret: 设置的密码, worker端的服务 必须是这个密码才能连接
  • ncpus: 要在本地计算机上启动的工作进程数
    • 不填此参数, 默认 本master 启动本机器所有进程作为内置worker; 
    • 值为0:本master不启动worker,只使用worker服务;
    • 其他整数值, 本master启动本机器对应数量的worker;   优先使用本地机器的worker

 

Worker端启动示例:

  1. worker端机器 先 安装 PP
  2. worker端 命令行执行  如下命令,即可启动成功;
ppserver.py -p 35000 -i 192.168.2.3 -s "mys" -d

命令行 worker服务启动参数含义及用法:

-d : debug模式,会打印所有运行日志,默认非此模式
-a : enable auto-discovery service;启动自动发现服务功能;
-i interface : interface to listen;监听的IP地址
-b broadcast : broadcast address for auto-discovery service
-p port : port to listen;监听的端口
-w nworkers : number of workers to start;启动的 worker数量,默认为 所有可以进程数;
-s secret : secret for authentication;认证的密码
-t seconds : timeout to exit if no connections with clients exist; 此服务如果没有服务端连接时,在超时 对应秒后,此服务进程停止;默认 不超时
-k seconds : socket timeout in seconds; 运行 worker时的超时时间,默认 不超时; 如 worker处理请求10s,此值设置9s;则停止处理此请求;
-P pid_file : file to write PID to;PID 文件指向位置

 

注意事项:

  • 因为 此服务 运行目的为 将原本在 本地进程执行 的耗时计算密集型任务 分散到 其他机器或CPU上执行, 而且 worker服务的实现方式 不用写任何代码,只需一行命令;   其实现方式 是将 需要执行的 函数 发送给远程worker服务进行执行;  这时 由于 worker是以 一个函数为执行 单位,如果 函数内部包含了 需要import的包或模块,则 需要 在函数内部 加上 from xxx import xxx; 或者 在submit 方法最后一个参数中加上 包名(submit中只能是包名,不能是模块名);
  • 注意 如果 被执行函数中 有任何 需要额外安装的第三方包,如 numpy,pandas,或 自己写的包 则 都需要在 worker服务环境中进行安装,否则 执行任务失败;
  • 在flask等等项目框架中,最好将 pp 的 master server启动放在app初始化中,和 mysql,mongo 等 一个级别; 否则  在 接口处理中 频繁的启动,销毁 会增加资源消耗 和 接口处理时间增加;
  • 注意worker服务 的 保活 可以使用 supervisor 或 docker 或k8s中;

 

和 celery的比较:

  • celery主要用在 异步处理上,主要为了 解耦业务逻辑,削峰填谷; 让 部分业务处理 放在 worker上执行后续处理;
  • pp主要用在 分布式计算上,和 阿里云的函数式计算 的应用场景相同; 都是用在  单进程或单机器 对 计算密集型任务 耗时很长,但是可以通过 分散到 多个机器上 解决耗时长 的问题时 使用;
  • celery用在 异步非及时性 需求中;  pp主要用在同步及时性高,且计算密集型的 需求中;

 

相关链接:

https://www.parallelpython.com/

https://wiki.python.org/moin/ParallelProcessing

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Paperback: 107 pages Publisher: Packt Publishing - ebooks Account (June 25, 2014) Language: English Develop efficient parallel systems using the robust Python environment Overview Demonstrates the concepts of Python parallel programming Boosts your Python computing capabilities Contains easy-to-understand explanations and plenty of examples In Detail Starting with the basics of parallel programming, you will proceed to learn about how to build parallel algorithms and their implementation. You will then gain the expertise to evaluate problem domains, identify if a particular problem can be parallelized, and how to use the Threading and Multiprocessor modules in Python. The Python Parallel (PP) module, which is another mechanism for parallel programming, is covered in depth to help you optimize the usage of PP. You will also delve into using Celery to perform distributed tasks efficiently and easily. Furthermore, you will learn about asynchronous I/O using the asyncio module. Finally, by the end of this book you will acquire an in-depth understanding about what the Python language has to offer in terms of built-in and external modules for an effective implementation of Parallel Programming. This is a definitive guide that will teach you everything you need to know to develop and maintain high-performance parallel computing systems using the feature-rich Python. What you will learn from this book Explore techniques to parallelize problems Integrate the Parallel Python module to implement Python code Execute parallel solutions on simple problems Achieve communication between processes using Pipe and Queue Use Celery Distributed Task Queue Implement asynchronous I/O using the Python asyncio module Create thread-safe structures Approach A fast, easy-to-follow and clear tutorial to help you develop Parallel computing systems using Python. Along with explaining the fundamentals, the book will also introduce you to slightly advanced concepts and will help you in implementing these techniques in the real world. Who this book is written for If you are an experienced Python programmer and are willing to utilize the available computing resources by parallelizing applications in a simple way, then this book is for you. You are required to have a basic knowledge of Python development to get the most of this book.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值