Python 多进程(multiprocessing)
来源:互联网 发布:学电脑编程 编辑:程序博客网 时间:2024/04/30 00:17
全局解释器锁GIL:
GIL全称全局解释器锁Global Interpreter Lock,GIL并不是Python的特性,它是实现Python解析器(CPython)时所引入的一个概念。
GIL是一把全局排他锁,同一时刻只有一个线程在运行。
毫无疑问全局锁的存在会对多线程的效率有不小的影响。甚至久几乎等于Python是个单线程的程序。
multiprocessing库的出现很大程度上是为了弥补thread库因为GIL而低效的缺陷。它完整的复制了一套thread所提供的接口方便迁移。唯一不同的就是它使用了多进程而不是多线程。每个进程有自己独立的GIL,因此不会出现进程间的GIL争抢。
multiprocessing是跨平台版本的多进程模块,它提供了一个Process类来代表一个进程对象,示例代码如下:
Example One:
import osfrom multiprocessing import Processimport timedef f(n): time.sleep(1) print(n*n)if __name__ == '__main__': start_time = time.time() for i in range(10): p = Process(target=f,args=[i,]) p.start() end_time = time.time() print('Total Time = {}'.format(end_time-start_time)) time.sleep(3) print('--------------------Complete Process---------------- ') begin_time = time.time() for i in range(10): f(i) last_time = time.time() print('Total Time = {}'.format(last_time-begin_time))
阅读全文
0 0
- Python多进程multiprocessing
- Python 多进程(multiprocessing)
- python 多进程 multiprocessing
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- Python多进程并发(multiprocessing)
- 31 Python 多进程-multiprocessing
- python multiprocessing多进程实例
- 【Python】multiprocessing多进程实例
- Python多进程multiprocessing.Pool()
- python multiprocessing多进程应用
- python 多进程模块 multiprocessing
- LeetCode:595 Big Countions
- C#--抽象工厂设计模式原理
- 无穷小放飞互联网行动即将开始!
- vs---编写第一个JavaScript程序
- 搭建AngularJS开发Webapp的开发环境-安装batarang
- Python 多进程(multiprocessing)
- fork() 与僵尸进程
- 每日英语阅读(五十五)
- Google OA题目:给出使得字符串B成为字符串A重复自己构成的字符串的子字符串,字符串A最少需重复的次数
- Java8 常用的Stream流操作
- 实战演练 text15 T1
- HDU2159FATE
- vb.net创建和调用DLL教程
- 【洛谷2017】 【USACO09DEC】晕牛Dizzy Cows