Python在处理多进程也做的很好,其提供了Multiprocessing来处理进程,就像处理线程一样,此模块包含了一个与Thread类非常类似的Process类,可以在任何平台上使用:
1. 源码Process
from multiprocessing import Process
import os
def work(identifier):
print('xsophiax, i am a process {}, pid: {}.'.format(identifier, os.getpid()))
def main():
processes = [
Process(target=work, args=(number,))
for number in range(5)
]
for process in processes:
process.start()
while processes:
processes.pop().join()
if __name__ == '__main__':
main()
输出结果:
xsophiax, i am a process 3, pid: 10148.
xsophiax, i am a process 1, pid: 9244.
xsophiax, i am a process 0, pid: 9900.
xsophiax, i am a process 2, pid: 8292.
xsophiax, i am a process 4, pid: 5840.
2. 源码Pipe
from multiprocessing import Process, Pipe
class CustomClass:
def __init__(self):
print('CustomClass.__init__')
def work(connection):
while True:
instance = connection.recv()
if instance:
print("CHLD: recv {}".format(instance))
else:
return
def main():
parent_conn, child_conn = Pipe()
child = Process(target=work, args=(child_conn,))
for item in (
42,
'some thing',
{'one': 1},
CustomClass(),
None
):
print("PRNT: Send {}:".format(item))
parent_conn.send(item)
child.start()
child.join()
if __name__ == '__main__':
main()
输出结果:
CustomClass.__init__
PRNT: Send 42:
PRNT: Send some thing:
PRNT: Send {'one': 1}:
PRNT: Send <__main__.CustomClass object at 0x00000000026F7D68>:
PRNT: Send None:
CHLD: recv 42
CHLD: recv some thing
CHLD: recv {'one': 1}
CHLD: recv <__mp_main__.CustomClass object at 0x00000000026A7EF0>
细心的同学能看到传递的自定义类示例其具有不同的地址,具体决定于进程;
3. 源码sharedctypes
from multiprocessing import Process, Value, Array
def f(n, a):
n.value = 3.1415927
for i in range(len(a)):
a[i] = -a[i]
def main():
num = Value('d', 0.0)
arr = Array('i', range(10))
p = Process(target=f, args=(num, arr))
p.start()
p.join()
print(num.value)
print(arr[:])
if __name__ == '__main__':
main()
输出结果
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
使用multiprocessing.sharedctypes时,您需要记住,正在使用的共享内存,因此为了避免数据顺坏的风险,你需要使用类似锁定原语。