multiprocessing 常用方法:

  • cpu_count():统计cpu核数

    multiprocessing.cpu_count()

  • active_children() 获取所有子进程

    multiprocessing.active_children()

  • preces() 创建一个进程对象

    multiprocessing.Preces(target=function_name, args=())

      target: 函数名
    
      args: 函数需要的参数,以tuple形式传入,一个参数时需(1,)

Preces 常用方法:

  • is_alive() 判断进程是否存在

  • run() 启动进程

  • start() 启动进程,会自动调用run方法,这个常用

  • join([timeout]) 等待进程结束或者直到超时

    • join() 方法说明:
    def def worker(interval):
    time.sleep(interval)
    print('hello world')
    P = multiprocessing.Process(target=worker, args=(5,))
    #-----------------------------------
    P.start()
    #设置timeout 设置超时时间
    print(P.is_alive())
    P.join(timeout=3)
    print('end main')
    ###
    True
    end main
    hello world
    #-----------------------------------
    P.start()
    P.alive()
    # 不调置timeout 超时时间
    P.join()
    print()
    ###
    True
    hello world
    end main
    #-----------------------------------
    结论:
    当join()不设置timeout时程序会一直等待上面的进程执行完成后再执行join()后面的代码
    当设置timeout时,无论上面的进程是否执行完成,程序运行到指定时间后就会执行后面的代码

Preces 常用属性

  • namd 进程名子

  • pid 进程的pid

python 多进程multiprocessing 模块的更多相关文章

  1. python多进程multiprocessing模块中Queue的妙用

    最近的部门RPA项目中,小爬为了提升爬虫性能,使用了Python中的多进程(multiprocessing)技术,里面需要用到进程锁Lock,用到进程池Pool,同时利用map方法一次构造多个proc ...

  2. Python(多进程multiprocessing模块)

    day31 http://www.cnblogs.com/yuanchenqi/articles/5745958.html 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分 ...

  3. Python之multiprocessing模块的使用

    作用:Python多进程处理模块,解决threading模块不能使用多个CPU内核,避免Python GIL(全局解释器)带来的计算瓶颈. 1.开启多进程的简单示例,处理函数无带参数 #!/usr/b ...

  4. 多进程Multiprocessing模块

    多进程 Multiprocessing 模块 先看看下面的几个方法: star() 方法启动进程, join() 方法实现进程间的同步,等待所有进程退出. close() 用来阻止多余的进程涌入进程池 ...

  5. Python 多进程 multiprocessing.Pool类详解

    Python 多进程 multiprocessing.Pool类详解 https://blog.csdn.net/SeeTheWorld518/article/details/49639651

  6. python之多进程multiprocessing模块

    process类介绍 multiprocessing 模块官方说明文档 Process 类用来描述一个进程对象.创建子进程的时候,只需要传入一个执行函数和函数的参数即可完成 Process 示例的创建 ...

  7. python 3 并发编程之多进程 multiprocessing模块

    一 .multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程. ...

  8. python ---多进程 Multiprocessing

    和 threading 的比较 多进程 Multiprocessing 和多线程 threading 类似, 他们都是在 python 中用来并行运算的. 不过既然有了 threading, 为什么 ...

  9. 转 Python 多进程multiprocessing.Process之satrt()和join()

    1. https://blog.csdn.net/wonengguwozai/article/details/80325745 今天项目中涉及到了使用多进程处理数据,在廖雪峰的python教程上学习了 ...

随机推荐

  1. LeetCode - 35. Search Insert Position(48ms)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. eclipse 创建Makefile工程生成多个执行文件

    1.创建Makefile工程 2.创建inc src Debug 目录 用于存放头文件源文件 3.编写Makefile 需要在有源文件的目标天剑Makefile文件,如下给出一个生成两个target的 ...

  3. python进阶训练

    1.列表,字典,集合解析 from random import randint #列表解析,选出大于0的元素 data=[randint(-10,10)for i in range(10)] resu ...

  4. STL应用——hdu1702(队列+堆栈)

    水题 练习一下堆栈和队列的使用 #include <iostream> #include <cstdio> #include <algorithm> #includ ...

  5. Mac上利用Aria2加速百度网盘下载

    百度网盘下载东西的速度那叫一个慢,特别是大文件,看着所需时间几个小时以上,让人很不舒服,本文记录自己在mac上利用工具Aria2加速的教程,windows下思路也是一样! 科普(可以不看) 这里顺带科 ...

  6. poj1789 Truck History最小生成树

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  7. IE提示是否只查看安全传送的网页内容

    IE选项-->安全-->点上面那个地球internet-->点下面那个 自定义级别-->找到“其他”-->显示混合内容,改为启用,重启打开下IE,就可以了

  8. 【题解】HAOI2011Problem b

    第一次接触莫比乌斯反演,总之脑子都快要炸掉了……好难啊!本蒟蒻表示根本无法理解呜呜呜呜呜……不过在机房DL的帮助下总算是磕磕绊绊的A掉了这一题: 这道题目要我们的求的是:(1)ΣiΣj [gcd(i, ...

  9. [洛谷P2584][ZJOI2006]GameZ游戏排名系统

    题目大意:同[洛谷P4291][HAOI2008]排名系统(双倍经验) 题解:略 卡点:无 C++ Code: #include <cstdio> #include <map> ...

  10. webpack 3.8 使用 extract-text-webpack-plugin 3.0 抽取css失败:You may need an appropriate loader to handle this file type.

    webpack 3.8.1 使用 extract-text-webpack-plugin 3.0.2 抽取css时失败,报错: ERROR in ./src/static/style/localTim ...