標籤:python 多進程
第一種開啟進程方式
#!/usr/bin/python# -*- coding:utf-8 -*-from multiprocessing import Processimport time, random, os# print(os.cpu_count())### # 定義一個任務# def piao(name):# print(‘%s is piaoing‘ % name)# time.sleep(3) # cpu阻塞,切換到主進程# print(‘%s is piao end‘ % name)### if __name__ == ‘__main__‘:# # target指定執行目標,args指定位置參數,指定為元組,kwargs指定字典# p1 = Process(target=piao, args=(‘lh‘,),name=‘<p1>‘) # 產生對象# # 建立子進程,賦值主進程地址空間,開啟一個子進程,建立進程需要時間,# # 發送系統調用,只是一個系統調用,指揮作業系統啟動子進# # 程,主進程並不等待子進程# p1.start() # 開啟子進程# # time.sleep(1) ## print(‘主進程‘)# # 誰先執行完誰先列印
第二種,自訂進程類
class Piao(Process): def __init__(self, name): super().__init__() # 重用父類方法, self.name = name def run(self): # 方法名必須為run print(‘%s is piaoing...‘ % self.name) time.sleep(3) print(‘%s is piao end‘ % self.name)if __name__ == ‘__main__‘: p1 = Piao(‘lh‘) p1.start() # 相當於p1.run() print(‘主進程‘)
鎖
#!/usr/bin/python# -*- coding:utf-8 -*-# import threading, time## v = 10## lock = threading.Lock() #只能有一個使用鎖# lock = threading.RLock() # 遞迴鎖### def task(arg):# time.sleep(2)# lock.accquire() # 申請使用鎖,其他線程等待# global v# v -= 1# print(v)# lock.release() # 交還釋放# for i in range(10):# t = threading.Thread(target=task, args=(i,))# t.start()# 多個人同時使用鎖,一批批操作# import threading, time## v = 10## lock = threading.BoundedSemaphore(3) #同時三個使用該鎖# # lock = threading.RLock() # 遞迴鎖### def task(arg):## lock.acquire() # 申請使用鎖,其他線程等待# time.sleep(1)# global v# v -= 1# print(v)# lock.release() # 交還釋放## for i in range(10):# t = threading.Thread(target=task, args=(i,))# t.start()# 時間鎖.解脫鎖的限制# import threading, time## v = 10## lock = threading.Event() # 同時三個使用該鎖### # lock = threading.RLock() # 遞迴鎖### def task(arg):# time.sleep(1)# lock.wait() # 鎖住多有線程# print(arg)### for i in range(10):# t = threading.Thread(target=task, args=(i,))# t.start()## while True:# value = input(‘>>>‘)# if value == ‘1‘:# lock.set()# lock.clear()#想怎麼鎖就怎麼鎖import threading, timev = 10lock = threading.Condition() # 同時三個使用該鎖# lock = threading.RLock() # 遞迴鎖def task(arg): time.sleep(1) lock.acquire() lock.wait()# 鎖住多有線程 print(arg) lock.release()for i in range(10): t = threading.Thread(target=task, args=(i,)) t.start()while True: value = input(‘>>>‘) lock.acquire() lock.notify(int(value)) lock.release()
進程對象的方法和屬性
from multiprocessing import Processimport time, random# def piao(name):# print(‘%s is piaoing‘ % name)# time.sleep(random.randint(1, 3))# print(‘%s is piao end‘ % name)### if __name__ == ‘__main__‘:# p1 = Process(target=piao, args=(‘egon‘,))# p2 = Process(target=piao, args=(‘lh‘,))# p3 = Process(target=piao, args=(‘apla‘,))# p4 = Process(target=piao, args=(‘tom‘,))## # 開啟順序不一定,誰先執行完誰列印# p1.start() # 不是阻塞操作,在發系統調用# p2.start() # 不是阻塞操作,在發系統調用# p3.start() # 不是阻塞操作,在發系統調用# p4.start() # 不是阻塞操作,在發系統調用## # p1.join() # 主進程等待p1執行完畢,hold# # p1.join() # 主進程等待# # p2.join()# # p3.join()# # p4.join()# p_l = [p1, p2, p3, p4]# for p in p_l:# p.start()## for p in p_l:# p.join()# print(‘主進程‘) # 每次都是主進程先列印,原因是cpu效能低# 守護進程def piao(name): print(‘%s is piaoing‘ % name) time.sleep(random.randint(1, 3)) print(‘%s is piao end‘ % name)if __name__ == ‘__main__‘: p1 = Process(target=piao, args=(‘egon‘,)) p1.daemon = True # 主進程運行完成後子進程執行完也會被回收 p1.start() print(p1.pid) print(p1.name) print(p1.is_alive()) # True p1.terminate() # 把p1幹掉,殭屍進程 time.sleep(0.5) print(p1.is_alive()) # False 不能立即幹掉進程 print(‘主進程‘)
進程同步之類比搶票
from multiprocessing import Process, Lockimport json, time, randomdef work(dbfile, name, lock): # lock.acquire() # 加鎖,誰先拿到,其他進程無法使用,加鎖是並發,join是排隊 with lock: # 上下文管理 with open(dbfile, encoding=‘utf-8‘) as f: dic = json.loads(f.read()) if dic[‘count‘] > 0: dic[‘count‘] -= 1 time.sleep(random.randint(1, 3)) # 類比網路延遲 with open(dbfile, ‘w‘, encoding=‘utf-8‘) as f: f.write(json.dumps(dic)) print(‘%s搶票成功!!!‘ % name) else: print(‘%s搶票失敗!!!‘ % name) # lock.release() # 鎖要釋放,不然其他的進程無法使用if __name__ == ‘__main__‘: lock = Lock() p_l = [] for i in range(100): p = Process(target=work, args=(‘a.txt‘, ‘使用者%s‘ % i, lock)) p_l.append(p) p.start()for p in p_l: p.join()print(‘主進程‘)
生產者消費者模型
from multiprocessing import Process, JoinableQueueimport random, timedef consumer(q, name): while True: # time.sleep(random.randint(1, 3)) res = q.get() # 一直在取狀態 q.task_done() # 執行完成 # if res is None: # break print(‘\033[41m消費者%s拿到%s\033[0m‘ % (name, res))def producer(seq, q, name): for item in seq: # time.sleep(random.randint(1, 3)) q.put(item) print(‘\033[42m生產者%s拿到%s\033[0m‘ % (name, item)) # q.put(None) q.join() print(‘**************>>‘) # 本行執行說明生產者執行完成,消費者把所有任務執行if __name__ == ‘__main__‘: q = JoinableQueue() c = Process(target=consumer, args=(q, ‘lh‘)) c.daemon=True #設定守護進程,朱金城結束,c就結束 c.start() seq = [‘包子%s‘ % i for i in range(10)] p = Process(target=producer, args=(seq, q, ‘廚師1‘)) p.start() # 生產者進程 p.join() # 主進程等待p結束,p等待c把資料取完,c一旦取完資料,p.join就不在阻塞,進而追進程結束 # ,主進程結束會回收守護進程c,而且此時c也沒有存在的必要只要join生產者就行,生產者在等待消費者取走 # c.join() print(‘主進程‘)# [i for i in range(10)] 列表產生式# [i for i in range(10) if i > 5] 列表產生式
本文出自 “11860872” 部落格,請務必保留此出處http://11870872.blog.51cto.com/11860872/1943642
python多進程