溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何在Python3.5中使用多進(jìn)程

發(fā)布時(shí)間:2021-04-02 16:08:25 來(lái)源:億速云 閱讀:121 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何在Python3.5中使用多進(jìn)程,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

具體如下:

如何在Python3.5中使用多進(jìn)程

進(jìn)程類(lèi):Process

如何在Python3.5中使用多進(jìn)程

示例及代碼:

如何在Python3.5中使用多進(jìn)程

(1)創(chuàng)建函數(shù)作為單進(jìn)程

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#創(chuàng)建函數(shù)并將其作為單個(gè)進(jìn)程
def worker(interval):
  n = 5    #進(jìn)程數(shù)
  while n>0:
    print("The time is :{0}".format(time.ctime()))   #初始化時(shí)間
    time.sleep(interval)    #睡眠時(shí)間
    n-=1
if __name__ == "__main__":
  # 創(chuàng)建進(jìn)程,target:調(diào)用對(duì)象,args:傳參數(shù)到對(duì)象
  p = multiprocessing.Process(target=worker,args=(2,))
  p.start()    #開(kāi)啟進(jìn)程
  print("進(jìn)程號(hào):",p.pid)
  print("進(jìn)程別名:",p.name)
  print("進(jìn)程存活狀態(tài):",p.is_alive())

運(yùn)行結(jié)果:

進(jìn)程號(hào): 6784
進(jìn)程別名: Process-1
進(jìn)程存活狀態(tài): True
The time is :Wed Nov  1 10:59:03 2017
The time is :Wed Nov  1 10:59:05 2017
The time is :Wed Nov  1 10:59:07 2017
The time is :Wed Nov  1 10:59:09 2017
The time is :Wed Nov  1 10:59:11 2017

(2)創(chuàng)建函數(shù)作為多進(jìn)程

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#創(chuàng)建函數(shù)作為多進(jìn)程
def work1(interval):
  print("work1...")
  time.sleep(interval)
  print("end work1...")
def work2(interval):
  print("work2...")
  time.sleep(interval)
  print("end work2...")
def work3(interval):
  print("work3...")
  time.sleep(interval)
  print("end work3...")
if __name__ == "__main__":
  p1 = multiprocessing.Process(target=work1,args=(1,))
  p2 = multiprocessing.Process(target=work2,args=(2,))
  p3 = multiprocessing.Process(target=work3,args=(3,))
  p1.start()
  p2.start()
  p3.start()
  print("The number of CPU is %d:"%(multiprocessing.cpu_count()))   #打印CPU核數(shù)
  for p in multiprocessing.active_children():     #循環(huán)打印子進(jìn)程的名稱(chēng)和pid
    print("子進(jìn)程名稱(chēng):%s,子進(jìn)程pid:%d" %(p.name,p.pid))
  print("ending....")

運(yùn)行結(jié)果:

The number of CPU is 4:
子進(jìn)程名稱(chēng):Process-2,子進(jìn)程pid:7108
子進(jìn)程名稱(chēng):Process-1,子進(jìn)程pid:1896
子進(jìn)程名稱(chēng):Process-3,子進(jìn)程pid:7952
ending....
work3...
work1...
work2...
end work1...
end work2...
end work3...

注:先運(yùn)行主進(jìn)程的內(nèi)容,再運(yùn)行子進(jìn)程

(3)將進(jìn)程定義成一個(gè)類(lèi)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#將進(jìn)程定義為一個(gè)類(lèi)
class ClockProcess(multiprocessing.Process):
  def __init__(self,interval):
    multiprocessing.Process.__init__(self)   #重構(gòu)了Process類(lèi)里面的構(gòu)造函數(shù)
    self.interval = interval
  def run(self):     #固定用run方法,啟動(dòng)進(jìn)程自動(dòng)調(diào)用run方法
    n = 5
    while n>0:
      print("The time is {0}".format(time.ctime()))
      time.sleep(self.interval)
      n-=1
if __name__ == "__main__":
  p = ClockProcess(2)
  p.start()

運(yùn)行結(jié)果:

The time is Wed Nov  1 11:31:28 2017
The time is Wed Nov  1 11:31:30 2017
The time is Wed Nov  1 11:31:32 2017
The time is Wed Nov  1 11:31:34 2017
The time is Wed Nov  1 11:31:36 2017

(4)Queue(隊(duì)列)實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)傳輸

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
#Queue是多進(jìn)程安全的隊(duì)列,可以使用實(shí)現(xiàn)多進(jìn)程之間的數(shù)據(jù)傳遞
def writer_proc(q):
  try:
    q.put(1,block=False)    #put方法插入數(shù)據(jù)到隊(duì)列中
  except:
    pass
def reader_proc(q):
  try:
    print(q.get(block=False))    #get方法從隊(duì)列中讀取并刪除一個(gè)元素
  except:
    pass
if __name__ == "__main__":
  q = multiprocessing.Queue()
  writer = multiprocessing.Process(target=writer_proc,args=(q,))
  writer.start()
  reader = multiprocessing.Process(target=reader_proc,args=(q,))
  reader.start()
  reader.join()
  writer.join()

運(yùn)行結(jié)果:

1

關(guān)于如何在Python3.5中使用多進(jìn)程就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI