欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3.5多進程原理與用法實例分析

 更新時間:2019年04月05日 09:20:35   作者:loveliuzz  
這篇文章主要介紹了Python3.5多進程原理與用法,結(jié)合實例形式分析了多進程的原理、單進程、多進程、進程類及進程隊列等相關(guān)定義與使用技巧,需要的朋友可以參考下

本文實例講述了Python3.5多進程原理與用法。分享給大家供大家參考,具體如下:

進程類:Process

示例及代碼:

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

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

運行結(jié)果:

進程號: 6784
進程別名: Process-1
進程存活狀態(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ù)作為多進程

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#創(chuàng)建函數(shù)作為多進程
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)打印子進程的名稱和pid
    print("子進程名稱:%s,子進程pid:%d" %(p.name,p.pid))
  print("ending....")

運行結(jié)果:

The number of CPU is 4:
子進程名稱:Process-2,子進程pid:7108
子進程名稱:Process-1,子進程pid:1896
子進程名稱:Process-3,子進程pid:7952
ending....
work3...
work1...
work2...
end work1...
end work2...
end work3...

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

(3)將進程定義成一個類

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#將進程定義為一個類
class ClockProcess(multiprocessing.Process):
  def __init__(self,interval):
    multiprocessing.Process.__init__(self)   #重構(gòu)了Process類里面的構(gòu)造函數(shù)
    self.interval = interval
  def run(self):     #固定用run方法,啟動進程自動調(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()

運行結(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(隊列)實現(xiàn)多進程數(shù)據(jù)傳輸

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
#Queue是多進程安全的隊列,可以使用實現(xiàn)多進程之間的數(shù)據(jù)傳遞
def writer_proc(q):
  try:
    q.put(1,block=False)    #put方法插入數(shù)據(jù)到隊列中
  except:
    pass
def reader_proc(q):
  try:
    print(q.get(block=False))    #get方法從隊列中讀取并刪除一個元素
  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()

運行結(jié)果:

1

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設(shè)計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • python?matplotlib用面積填充實現(xiàn)lmplot的代碼示例

    python?matplotlib用面積填充實現(xiàn)lmplot的代碼示例

    這篇文章主要介紹了python?matplotlib如何用面積填充實現(xiàn)lmplot,文章通過代碼示例介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴可以參考閱讀
    2023-07-07
  • Python實現(xiàn)簡易的圖書管理系統(tǒng)

    Python實現(xiàn)簡易的圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Python實現(xiàn)簡易的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • python lambda函數(shù)及三個常用的高階函數(shù)

    python lambda函數(shù)及三個常用的高階函數(shù)

    這篇文章主要介紹了python lambda函數(shù)及三個常用的高階函數(shù),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 詳解Python下ftp上傳文件linux服務(wù)器

    詳解Python下ftp上傳文件linux服務(wù)器

    本篇文章給大家總結(jié)了Python下ftp上傳文件linux服務(wù)器的詳細功能代碼,有需要的朋友參考學(xué)習(xí)下。
    2018-06-06
  • Python自動化辦公之Excel數(shù)據(jù)的寫入

    Python自動化辦公之Excel數(shù)據(jù)的寫入

    這篇文章主要為大家詳細介紹一下Python中excel的寫入模塊- xlsxwriter,并利用該模塊實現(xiàn)Excel數(shù)據(jù)的寫入,感興趣的小伙伴可以了解一下
    2022-05-05
  • python實現(xiàn)簡單猜單詞游戲

    python實現(xiàn)簡單猜單詞游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單猜單詞游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 一文詳解Python中復(fù)合語句的用法

    一文詳解Python中復(fù)合語句的用法

    復(fù)合語句是包含其它語句(語句組)的語句;它們會以某種方式影響或控制所包含其它語句的執(zhí)行。通常,復(fù)合語句會跨越多行,雖然在某些簡單形式下整個復(fù)合語句也可能包含于一行之內(nèi)。本文就來講講Python中復(fù)合語句的使用
    2022-07-07
  • 詳解Numpy數(shù)組轉(zhuǎn)置的三種方法T、transpose、swapaxes

    詳解Numpy數(shù)組轉(zhuǎn)置的三種方法T、transpose、swapaxes

    這篇文章主要介紹了詳解Numpy數(shù)組轉(zhuǎn)置的三種方法T、transpose、swapaxes,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 詳解Python遍歷字典的鍵和值

    詳解Python遍歷字典的鍵和值

    這篇文章主要通過一些簡單的示例為大家介紹一下Python中遍歷字典的鍵和值的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-03-03
  • PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享

    PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享

    本文給大家介紹PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享,程序?qū)崿F(xiàn)了一鍵抓取帖子全部郵箱和分頁抓取郵箱兩個功能,感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04

最新評論