Python 创建进程的三种方式

第一种方式:使用os模块中的fork方式实现多进程

Python
# -*- 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模块创建多进程

Python
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类来代表进程池对象

Python
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进程间通信

Python
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进程间通信

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



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值