首页 > 代码库 > Python的多进程编程
Python的多进程编程
考虑到多线程,都在一个主进程中共享栈变量,在操作同一个局部变量时可能出现絮乱的现象,即使加锁也容易出现死锁的现象,小编在这里再次记录下多进程编程,废话不多说,直接上代码:
#!/usr/bin/env python #encoding: utf-8 import multiprocessing import time def process_one(interval): n =5 while n >0: print ("the time is {0}".format(time.ctime())) time.sleep(interval) n -= 1 if __name__=="__main__": process_test = multiprocessing.Process(target = process_one,args = (2,)) process_test.start() print "process_test.pid:", process_test.pid print "process_test.name:",process_test.name print "process_test.is_alive",process_test.is_alive()
在命令行运行结果:
C:\Python27>python mul_process.py process_test.pid: 88492 process_test.name: Process-1 process_test.is_alive True the time is Sun Jul 16 19:38:24 2017 the time is Sun Jul 16 19:38:26 2017 the time is Sun Jul 16 19:38:28 2017 the time is Sun Jul 16 19:38:30 2017 the time is Sun Jul 16 19:38:32 2017
同时开启3个进程:
#!/usr/bin/env python #encoding: utf-8 import multiprocessing import time def process_one(interval): time.sleep(interval) print "start process_one\n" print ("process_one work time is {0}\n".format(time.ctime())) print "end process_one\n" def process_two(interval): time.sleep(interval) print "start process_two\n" print ("process_two work time is {0}\n".format(time.ctime())) print "end process_two\n" def process_three(interval): time.sleep(interval) print "start process_three\n" print ("process_three work time is {0}\n".format(time.ctime())) print "end process_three\n" if __name__=="__main__": process_test_1 = multiprocessing.Process(target = process_one,args = (1,)) process_test_2 = multiprocessing.Process(target = process_two,args = (2,)) process_test_3 = multiprocessing.Process(target = process_three,args = (3,)) process_test_1.start() process_test_2.start() process_test_3.start() print "the number of CPU is :"+str(multiprocessing.cpu_count())+"\n" for p in multiprocessing.active_children(): print "child p.name "+p.name+"\np.id "+ str(p.pid)+"\n" print "end!!!!!!!!!!!!!!"
运行结果:
C:\Python27>python mul_process.py the number of CPU is :2 child p.name Process-3 p.id 101572 child p.name Process-2 p.id 101420 child p.name Process-1 p.id 99852 end!!!!!!!!!!!!!! start process_one process_one work time is Sun Jul 16 20:05:07 2017 end process_one start process_two process_two work time is Sun Jul 16 20:05:08 2017 end process_two start process_three process_three work time is Sun Jul 16 20:05:09 2017 end process_three
将进程封装为类:
#!/usr/bin/env python #encoding: utf-8 import multiprocessing import time class ClockProcess(multiprocessing.Process): def __init__(self,interval): multiprocessing.Process.__init__(self) self.interval = interval def run(self): n=5 while n>0: print "the time is {0}".format(time.ctime()) time.sleep(self.interval) n -= 1 if __name__=="__main__": process_test_1 = ClockProcess(2) process_test_1.start()
温馨提示:进程p调用start()时,自动调用run()
运行结果:
C:\Python27>python mul_process.py the time is Sun Jul 16 20:11:59 2017 the time is Sun Jul 16 20:12:01 2017 the time is Sun Jul 16 20:12:03 2017 the time is Sun Jul 16 20:12:05 2017 the time is Sun Jul 16 20:12:07 2017
探究daemon程序对比结果
#!/usr/bin/env python #encoding: utf-8 import multiprocessing import time def process_one(interval): print "process_one start time :{0}".format(time.ctime()) time.sleep(interval) print "process_one end time :{0}".format(time.ctime()) if __name__=="__main__": process_test = multiprocessing.Process(target = process_one,args = (2,)) process_test.daemon = True process_test.start() print "end!!!!!!!!!!!!!!!"
执行结果:
C:\Python27>python mul_process.py
end!!!!!!!!!!!!!!!
怎么回事???开启的进程怎么没有运行?
答:因子进程设置了daemon属性,主进程结束,它们就随着结束了
#!/usr/bin/env python #encoding: utf-8 import multiprocessing import time def process_one(interval): print "process_one start time :{0}".format(time.ctime()) time.sleep(interval) print "process_one end time :{0}".format(time.ctime()) if __name__=="__main__": process_test = multiprocessing.Process(target = process_one,args = (2,)) process_test.daemon = True process_test.start() process_test.join() print "end!!!!!!!!!!!!!!!"
运行结果:
提示这里跟多线程相似
C:\Python27>python mul_process.py
process_one start time :Sun Jul 16 20:21:34 2017
process_one end time :Sun Jul 16 20:21:36 2017
end!!!!!!!!!!!!!!!
今天暂时写到这儿,调试 程序遇到一点问题
原因大致如下,因为我的程序是在Windows运行的,待代码在linux上跑通之后再更新
In Windows, multiprocessing
uses pickle to transfer objects between processes. Socket objects can not be pickled, hence the problem that you see.
Your code does work in Linux, and that is because multiprocessing
uses fork
on that platform. The child of a forked process inherits the parent‘s file handles, of which one is the socket.
Python的多进程编程