简单谈谈python中的多进程

yipeiwu_com5年前Python基础

进程是由系统自己管理的。

1:最基本的写法

from multiprocessing import Pool

def f(x):
  return x*x

if __name__ == '__main__':
  p = Pool(5)
  print(p.map(f, [1, 2, 3]))
[1, 4, 9]

2、实际上是通过os.fork的方法产生进程的

unix中,所有进程都是通过fork的方法产生的。

multiprocessing Process
os

info(title):
  title
  , __name__
  (os, ): , os.getppid()
  , os.getpid()

f(name):
  info()
  , name

__name__ == :
  info()
  p = Process(=f, =(,))
  p.start()
  p.join()

3、线程共享内存

threading

run(info_list,n):
  info_list.append(n)
  info_list

__name__ == :
  info=[]
  i ():
    p=threading.Thread(=run,=[info,i])
    p.start()
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

进程不共享内存:

multiprocessing Process
run(info_list,n):
  info_list.append(n)
  info_list

__name__ == :
  info=[]
  i ():
    p=Process(=run,=[info,i])
    p.start()
[1]
[2]
[3]
[0]
[4]
[5]
[6]
[7]
[8]
[9]

若想共享内存,需使用multiprocessing模块中的Queue

multiprocessing Process, Queue
f(q,n):
  q.put([n,])

__name__ == :
  q=Queue()
  i ():
    p=Process(=f,=(q,i))
    p.start()
  :
    q.get()

4、锁:仅是对于屏幕的共享,因为进程是独立的,所以对于多进程没有用

multiprocessing Process, Lock
f(l, i):
  l.acquire()
  , i
  l.release()

__name__ == :
  lock = Lock()

  num ():
    Process(=f, =(lock, num)).start()
hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9

5、进程间内存共享:Value,Array

multiprocessing Process, Value, Array

f(n, a):
  n.value = i ((a)):
    a[i] = -a[i]

__name__ == :
  num = Value(, )
  arr = Array(, ())

  num.value
  arr[:]

  p = Process(=f, =(num, arr))
  p.start()
  p.join()
0.0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

#manager共享方法,但速度慢

multiprocessing Process, Manager

f(d, l):
  d[] = d[] = d[] = l.reverse()

__name__ == :
  manager = Manager()

  d = manager.dict()
  l = manager.list(())

  p = Process(=f, =(d, l))
  p.start()
  p.join()

  d
  l
# print '-------------'这里只是另一种写法
# print pool.map(f,range(10))
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#异步:这种写法用的不多

multiprocessing Pool
time
f(x):
  x*x
  time.sleep()
  x*x

__name__ == :
  pool=Pool(=)
  res_list=[]
  i ():
    res=pool.apply_async(f,[i])  res_list.append(res)

  r res_list:
    r.get(timeout=10) #超时时间

同步的就是apply

相关文章

python多线程编程中的join函数使用心得

python多线程编程中的join函数使用心得

今天去辛集买箱包,下午挺晚才回来,又是恶心又是头痛。恶心是因为早上吃坏东西+晕车+回来时看到车祸现场,头痛大概是烈日和空调混合刺激而成。没有时间没有精神没有力气学习了,这篇博客就说说py...

Python Unittest自动化单元测试框架详解

Python Unittest自动化单元测试框架详解

本文实例为大家分享了Python Unittest自动化单元测试框架的具体代码,供大家参考,具体内容如下 1、python 测试框架(本文只涉及 PyUnit) 参考地址 2、环境准备...

Python 最强编辑器详细使用指南(PyCharm )

Python 最强编辑器详细使用指南(PyCharm )

作者:Jahongir Rahmonov 机器之心编译 参与:魔王 PyCharm 是一种 Python IDE,可以帮助程序员节约时间,提高生产效率。那么具体如何使用呢?本文从 PyC...

Python 字符串操作方法大全

1、去空格及特殊符号复制代码 代码如下:s.strip().lstrip().rstrip(',')2、复制字符串复制代码 代码如下:#strcpy(sStr1,sStr2)sStr1...

pandas object格式转float64格式的方法

在数据处理过程中 比如从CSV文件中导入数据 data_df = pd.read_csv("names.csv") 在处理之前一定要查看数据的类型 data_df.info()...