Python学习之多进程(1)

对于unix,linux等系统可以使用fork来创建一个子进程,子进程复制父进程里面的内容,然后分别在父进程和子进程里面返回.同时还可以用os模块里面的getpid和getppid获得当前进程号.

import os
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))

但是对于使用windows系统的人就没有办法创建进程了吗?当然不是,我们可以用multiprocessing模块来进行跨平台的多进程:

有一个地方需要注意:为什么一定要加上__name__ == __main__因为将name打印出来之后你会发现在调用p.start后会有一个name是__mp__main的模块调用我们的模块,这很可能是multiprocessing的name缩写,如果不加判断那么进程就会反复被创建以至于陷入无限循环出错.

from multiprocessing import Process
import os
def Run_proc(name):#running the child process
    print('Now Run the child Process%s (%s),the parent process(%s)'%(name,os.getpid(),os.getppid()))

print(__name__)
if __name__ == '__main__':
    p = Process(target = Run_proc,args = ('test',))
    print('The parent process(%s)'%os.getpid())
    print('Now the child process start')
    p.start()
    p.join()##等到子进程结束
    print('the child Process end ')

使用Pool池:

在此吐槽下Python解释器对错误的检测…老提示if __name…..这一句是invalid的,最后发现是print语句少一个括号….所以要注意上文的语法错误会影响下文,而且还不会提示你….

from multiprocessing import Pool
import os, time, random
def Run_proc(name):#running the child process
    print('Run the child Process%s (%s),the parent process(%s)'%(name,os.getpid(),os.getppid()))
    start = time.time()
    time.sleep(random.random()*3)
    end = time.time()
    print('the child Process %s end,lasting time(%d)'%(name,end-start))


if __name__=='__main__':
    p = Pool(4)
    for i in range(5):
        p.apply_async(Run_proc,args = (i,))
    p.close()
    p.join()
    print('the child Process end ')

进程之间的通信:利用Queue进行通信.注意,使用lsit是不行的,它们在不同进程之间不是同一块内存!!

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()

你可能感兴趣的:(Python学习之多进程(1))