进程和线程

作者 新城 日期 2017-08-31
进程和线程

(进程和线程)[https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319272686365ec7ceaeca33428c914edf8f70cca383000]

多任务的实现有3种方式:

多进程模式;
多线程模式;
多进程+多线程模式。
Python既支持多进程,又支持多线程

小结:
线程是最小的执行单元,而进程由至少一个线程组成。如何调度进程和线程,
完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间

(多进程)[https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431927781401bb47ccf187b24c3b955157bb12c5882d000#0]

Unix/Linux操作系统提供了一个fork()系统调用

普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次,
因为操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),然后,
分别在父进程和子进程内返回

1
2
3
4
5
6
7
8
9
import os       #window执行失败  window没有fork函数

print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac:
pid = os.fork()
if pid == 0:
print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else:
print('I (%s) just created a child process (%s).' % (os.getpid(), pid))

输出Process (876) start…
I (876) just created a child process (877).
I am child process (877) and my parent is 876.

multiprocessing

multiprocessing模块就是跨平台版本的多进程模块

1
2
3
4
5
6
7
8
9
10
11
12
13
from multiprocessing import Process     #导入跨平台模块
import os

def run_proc(name): # 子进程要执行的代码
print('Run child process %s (%s)...' % (name, os.getpid()))

if __name__=='__main__':
print('Parent process %s.' % os.getpid())
p = Process(target=run_proc, args=('test',))
print('Child process will start.')
p.start()
p.join()
print('Child process end.')

Parent process 928.
Process will start.
Run child process test (929)…
Process end.

join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步

pool 进程池

如果要启动大量的子进程,可以用进程池的方式批量创建子进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from multiprocessing import Pool
import os, time, random

def long_time_task(name):
print('Run task %s (%s)...' % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print('Task %s runs %0.2f seconds.' % (name, (end - start)))

if __name__=='__main__':
print('Parent process %s.' % os.getpid())
p = Pool(4)
for i in range(5):
p.apply_async(long_time_task, args=(i,))
print('Waiting for all subprocesses done...')
p.close()
p.join()
print('All subprocesses done.')

执行结果:

Parent process 669.
Waiting for all subprocesses done…
Run task 0 (671)…
Run task 1 (672)…
Run task 2 (673)…
Run task 3 (674)…
Task 2 runs 0.14 seconds.
Run task 4 (673)…
Task 1 runs 0.27 seconds.
Task 3 runs 0.86 seconds.
Task 0 runs 1.41 seconds.
Task 4 runs 1.91 seconds.
All subprocesses done.

代码解读

对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),
调用close()之后就不能继续添加新的Process了

请注意输出的结果,task 0,1,2,3是立刻执行的,而task 4要等待前面某个task完成后才执行,
这是因为Pool的默认大小在我的电脑上是4,因此,最多同时执行4个进程。这是Pool有意设计的限制,
并不是操作系统的限制。如果改成:

p = Pool(5)

就可以同时跑5个进程。

由于Pool的默认大小是CPU的核数,如果你不幸拥有8核CPU,你要提交至少9个子进程才能看到上面的等待效果。

子进程

subprocess模块可以让我们非常方便地启动一个子进程,然后控制其输入和输出

1
2
3
4
5
import subprocess

print('$ nslookup www.python.org')
r = subprocess.call(['nslookup', 'www.python.org'])
print('Exit code:', r)

相当于命令行运行 nslookup www.python.org

1
2
3
$ nslookup www.python.org
Server: 192.168.19.4
Address: 192.168.19.4#53

如果子进程还需要输入,则可以通过communicate()方法输入:

1
2
3
4
5
6
7
import subprocess

print('$ nslookup')
p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate(b'set q=mx\npython.org\nexit\n')
print(output.decode('utf-8'))
print('Exit code:', p.returncode)

上面的代码相当于在命令行执行命令nslookup,然后手动输入:

set q=mx
python.org
exit

进程间通信

Process之间肯定是需要通信的,操作系统提供了很多机制来实现进程间的通信。Python的multiprocessing模块包装了底层的机制,提供了Queue、Pipes等多种方式来交换数据。

我们以Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据

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
from multiprocessing import Process, Queue
import os, time, random

# 写数据进程执行的代码:
def write(q):
print('Process to write: %s' % os.getpid())
for value in ['A', 'B', 'C']:
print('Put %s to queue...' % value)
q.put(value)
time.sleep(random.random())

# 读数据进程执行的代码:
def read(q):
print('Process to read: %s' % os.getpid())
while True:
value = q.get(True)
print('Get %s from queue.' % value)

if __name__=='__main__': # 父进程创建Queue,并传给各个子进程:
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,)) # 启动子进程pw,写入:
pw.start() # 启动子进程pr,读取:
pr.start() # 等待pw结束:
pw.join() # pr进程里是死循环,无法等待其结束,只能强行终止:
pr.terminate()

执行结果

1
2
3
4
5
6
7
8
Process to write: 50563
Put A to queue...
Process to read: 50564
Get A from queue.
Put B to queue...
Get B from queue.
Put C to queue...
Get C from queue.

小结

Unix/Linux下,可以使用fork()调用实现多进程。

要实现跨平台的多进程,可以使用multiprocessing模块。

进程间通信是通过Queue、Pipes等实现的。