1. 程式人生 > >Python的多進程編程

Python的多進程編程

rgs cloc Coding 死鎖 命令行 執行 sel targe get

考慮到多線程,都在一個主進程中共享棧變量,在操作同一個局部變量時可能出現絮亂的現象,即使加鎖也容易出現死鎖的現象,小編在這裏再次記錄下多進程編程,廢話不多說,直接上代碼:

#!/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的多進程編程