第一种方式:使用os模块中的fork方式实现多进程
# -*- coding: utf-8 -*- """ @Time: 2018/1/20 @Author: songhao @微信公众号: zero<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/python" title="View all posts in python" target="_blank">python</a></span> @File: jincheng.py """ # 第一种方式:使用<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/os" title="View all posts in os" target="_blank">os</a></span>模块中的fork方式实现多进程 import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/os" title="View all posts in os" target="_blank">os</a></span> if __name__ == '__main__': print('current Process ({}) start ...'.format(<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/os" title="View all posts in os" target="_blank">os</a></span>.getpid())) pid = os.fork() if pid < 0: print('error in fork') elif pid == 0: print('I am child process({0}) and my parent process is ({1})'.format(os.getpid(), os.getppid())) else: print('I({0}) created a chlid process ({1}).'.format(os.getpid(),pid))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/20
@Author: songhao
@微信公众号: zeropython
@File: jincheng.py
"""
# 第一种方式:使用os模块中的fork方式实现多进程
import
os
if
__name__
==
'__main__'
:
print
(
'current Process ({}) start ...'
.
format
(
os
.
getpid
(
)
)
)
pid
=
os
.
fork
(
)
if
pid
<
0
:
print
(
'error in fork'
)
elif
pid
==
0
:
print
(
'I am child process({0}) and my parent process is ({1})'
.
format
(
os
.
getpid
(
)
,
os
.
getppid
(
)
)
)
else
:
print
(
'I({0}) created a chlid process ({1}).'
.
format
(
os
.
getpid
(
)
,
pid
)
)
|
输出:
第二种方法:使用multiprocessing模块创建多进程
import os from <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/multiprocessing" title="View all posts in multiprocessing" target="_blank">multiprocessing</a></span> import Process # 子进程要执行的代码 def run_proc(name): print('Child process {0} ({1}) Running...'.format(name, os.getpid())) if __name__ == '__main__': print('Parent process %s.' % os.getpid()) p_list=[] for i in range(5): p = Process(target=run_proc, args=(str(i),)) p_list.append(p) print('Process will start.') p_list[i].start() for p in p_list: p.join() print('Process end.')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
os
from
multiprocessing
import
Process
# 子进程要执行的代码
def
run_proc
(
name
)
:
print
(
'Child process {0} ({1}) Running...'
.
format
(
name
,
os
.
getpid
(
)
)
)
if
__name__
==
'__main__'
:
print
(
'Parent process %s.'
%
os
.
getpid
(
)
)
p_list
=
[
]
for
i
in
range
(
5
)
:
p
=
Process
(
target
=
run_proc
,
args
=
(
str
(
i
)
,
)
)
p_list
.
append
(
p
)
print
(
'Process will start.'
)
p_list
[
i
]
.
start
(
)
for
p
in
p_list
:
p
.
join
(
)
print
(
'Process end.'
)
|
输出结果:
multiprocessing模块提供了一个Pool类来代表进程池对象
from multiprocessing import Pool import os, time, random def run_task(name): print 'Task %s (pid = %s) is running...' % (name, os.getpid()) time.sleep(random.random() * 3) print 'Task %s end.' % name if __name__=='__main__': print 'Current process %s.' % os.getpid() p = Pool(processes=3) for i in range(5): p.apply_async(run_task, args=(i,)) print 'Waiting for all subprocesses done...' p.close() p.join() print 'All subprocesses done.'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from
multiprocessing
import
Pool
import
os
,
time
,
random
def
run_task
(
name
)
:
print
'Task %s (pid = %s) is running...'
%
(
name
,
os
.
getpid
(
)
)
time
.
sleep
(
random
.
random
(
)
*
3
)
print
'Task %s end.'
%
name
if
__name__
==
'__main__'
:
print
'Current process %s.'
%
os
.
getpid
(
)
p
=
Pool
(
processes
=
3
)
for
i
in
range
(
5
)
:
p
.
apply_async
(
run_task
,
args
=
(
i
,
)
)
print
'Waiting for all subprocesses done...'
p
.
close
(
)
p
.
join
(
)
print
'All subprocesses done.'
|
# Queue进程间通信
Queue进程间通信 from multiprocessing import Process, Queue import os, time, random # 写数据进程执行的代码: def proc_write(q,urls): print('Process(%s) is writing...' % os.getpid()) for url in urls: q.put(url) print('Put %s to queue...' % url) time.sleep(random.random()) # 读数据进程执行的代码: def proc_read(q): print('Process(%s) is reading...' % os.getpid()) while True: url = q.get(True) print('Get %s from queue.' % url) if __name__=='__main__': # 父进程创建Queue,并传给各个子进程: q = Queue() proc_writer1 = Process(target=proc_write, args=(q,['url_1', 'url_2', 'url_3'])) proc_writer2 = Process(target=proc_write, args=(q,['url_4','url_5','url_6'])) proc_reader = Process(target=proc_read, args=(q,)) # 启动子进程proc_writer,写入: proc_writer1.start() proc_writer2.start() # 启动子进程proc_reader,读取: proc_reader.start() # 等待proc_writer结束: proc_writer1.join() proc_writer2.join() # proc_reader进程里是死循环,无法等待其结束,只能强行终止: proc_reader.terminate()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Queue进程间通信
from
multiprocessing
import
Process
,
Queue
import
os
,
time
,
random
# 写数据进程执行的代码:
def
proc_write
(
q
,
urls
)
:
print
(
'Process(%s) is writing...'
%
os
.
getpid
(
)
)
for
url
in
urls
:
q
.
put
(
url
)
print
(
'Put %s to queue...'
%
url
)
time
.
sleep
(
random
.
random
(
)
)
# 读数据进程执行的代码:
def
proc_read
(
q
)
:
print
(
'Process(%s) is reading...'
%
os
.
getpid
(
)
)
while
True
:
url
=
q
.
get
(
True
)
print
(
'Get %s from queue.'
%
url
)
if
__name__
==
'__main__'
:
# 父进程创建Queue,并传给各个子进程:
q
=
Queue
(
)
proc_writer1
=
Process
(
target
=
proc_write
,
args
=
(
q
,
[
'url_1'
,
'url_2'
,
'url_3'
]
)
)
proc_writer2
=
Process
(
target
=
proc_write
,
args
=
(
q
,
[
'url_4'
,
'url_5'
,
'url_6'
]
)
)
proc_reader
=
Process
(
target
=
proc_read
,
args
=
(
q
,
)
)
# 启动子进程proc_writer,写入:
proc_writer1
.
start
(
)
proc_writer2
.
start
(
)
# 启动子进程proc_reader,读取:
proc_reader
.
start
(
)
# 等待proc_writer结束:
proc_writer1
.
join
(
)
proc_writer2
.
join
(
)
# proc_reader进程里是死循环,无法等待其结束,只能强行终止:
proc_reader
.
terminate
(
)
|
pipe进程间通信
import multiprocessing import random import time,os def proc_send(pipe,urls): for url in urls: print "Process(%s) send: %s" %(os.getpid(),url) pipe.send(url) time.sleep(random.random()) def proc_recv(pipe): while True: print "Process(%s) rev:%s" %(os.getpid(),pipe.recv()) time.sleep(random.random())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
multiprocessing
import
random
import
time
,
os
def
proc_send
(
pipe
,
urls
)
:
for
url
in
urls
:
print
"Process(%s) send: %s"
%
(
os
.
getpid
(
)
,
url
)
pipe
.
send
(
url
)
time
.
sleep
(
random
.
random
(
)
)
def
proc_recv
(
pipe
)
:
while
True
:
print
"Process(%s) rev:%s"
%
(
os
.
getpid
(
)
,
pipe
.
recv
(
)
)
time
.
sleep
(
random
.
random
(
)
)
|