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

Python mutiprocessing多線程池pool操作示例

 更新時間:2019年01月30日 12:10:13   作者:徐為波  
這篇文章主要介紹了Python mutiprocessing多線程池pool操作,結合實例形式分析了Python多線程模塊multiprocessing進程池相關操作技巧,需要的朋友可以參考下

本文實例講述了Python mutiprocessing多線程池pool操作。分享給大家供大家參考,具體如下:

python — mutiprocessing 多線程 pool

腳本代碼:

root@72132server:~/python/multiprocess# ls
multiprocess_pool.py multprocess.py
root@72132server:~/python/multiprocess# cat multiprocess_pool.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import multiprocessing
import sys,os,time
result = []#把運行的進程池放入,空的列表
def run(msg):#定義正在處理進程編號數(shù)的函數(shù)功能
  print 'threading number:%s %s' %(msg,os.getpid())#打印正在處理的進程編號數(shù)與對應的系統(tǒng)進程號
  time.sleep(2)
p = multiprocessing.Pool(processes = 25)#綁定事例,同時執(zhí)行25個線程
for i in range(100):
  result.append(p.apply_async(run,('%s' %i,)))#異步傳輸正在運行的進程數(shù)字號碼
p.close()#關閉正在運行的25個進程
#p.join()
for res in result:#獲取運行結果
  res.get(timeout=5)
root@72132server:~/python/multiprocess#

運行情況:

1)腳本運行

root@72132server:~/python/multiprocess# python multiprocess_pool.py
threading number:0 27912
threading number:1 27915
threading number:2 27913
threading number:3 27916
threading number:4 27917
threading number:5 27918
threading number:6 27919
threading number:7 27920
threading number:8 27922
threading number:9 27923
threading number:10 27924
threading number:11 27925
threading number:12 27926
threading number:13 27927
threading number:14 27928
threading number:15 27914
threading number:16 27929
threading number:17 27921
threading number:18 27930
threading number:19 27931
threading number:20 27932
threading number:21 27934
threading number:22 27935
threading number:23 27936
threading number:24 27933
threading number:25 27912
threading number:26 27915
threading number:27 27917
threading number:28 27918
threading number:29 27916
threading number:30 27913
threading number:31 27922
threading number:32 27919
threading number:33 27920
threading number:34 27923
threading number:35 27924
threading number:36 27925
threading number:37 27927
threading number:38 27921
threading number:39 27930
threading number:40 27932
threading number:41 27934
threading number:42 27935
threading number:43 27926
threading number:44 27931
threading number:45 27928
threading number:46 27929
threading number:47 27914
threading number:48 27933
threading number:49 27936
threading number:50 27912
threading number:51 27915

2)進程查看(25個進程同時運行)

root@72132server:~/python/multiprocess# ps -ef | grep multi
root   27905 23930 0 22:39 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   27911 20609 1 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27912 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27913 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27914 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27915 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27916 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27917 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27918 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27919 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27920 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27921 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27922 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27923 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27924 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27925 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27926 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27927 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27928 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27929 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27930 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27931 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27932 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27933 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27934 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27935 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27936 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27941 23930 0 22:39 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   27911 20609 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27912 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27913 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27914 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27915 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27916 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27917 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27918 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27919 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27920 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27921 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27922 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27923 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27924 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27925 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27926 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27927 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27928 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27929 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27930 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27931 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27932 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27933 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27934 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27935 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27936 27911 0 22:39 pts/1  00:00:00 python multiprocess_pool.py
root   27943 23930 0 22:39 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess#

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

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

相關文章

  • Python實現(xiàn)多任務版的udp聊天器

    Python實現(xiàn)多任務版的udp聊天器

    這篇文章主要為大家詳細介紹了Python實現(xiàn)多任務版的udp聊天器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • python中filter,map,reduce的作用

    python中filter,map,reduce的作用

    這篇文章主要介紹了python中filter,map,reduce的作用,文章首先通過map函數(shù)展開,map主要作用是計算一個序列或者多個序列進行函數(shù)映射之后的值,感興趣的朋友可以參考一下
    2022-06-06
  • Python?matplotlib?plotly繪制圖表詳解

    Python?matplotlib?plotly繪制圖表詳解

    plotly本身是個生態(tài)非常復雜的繪圖工具,它對很多編程語言提供接口。交互式和美觀易用應該是?Plotly?最大的優(yōu)勢,而?Matplotlib?的特點則是可定制化程度高,但語法也相對難學,各有優(yōu)缺點。本文將通過示例詳細講解二者是如何繪制圖表的,需要的可以參考一下
    2022-03-03
  • 在python中如何建立一個自己的包

    在python中如何建立一個自己的包

    這篇文章主要介紹了在python中如何建立一個自己的包,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python的print函數(shù)如何覆蓋打印

    Python的print函數(shù)如何覆蓋打印

    這篇文章主要介紹了Python的print函數(shù)如何覆蓋打印問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python將txt等文件中的數(shù)據(jù)讀為numpy數(shù)組的方法

    python將txt等文件中的數(shù)據(jù)讀為numpy數(shù)組的方法

    今天小編就為大家分享一篇python將txt等文件中的數(shù)據(jù)讀為numpy數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python2 與 python3 實現(xiàn)共存的方法

    python2 與 python3 實現(xiàn)共存的方法

    這篇文章主要介紹了python2 與 python3 實現(xiàn)共存的方法,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • 對python 通過ssh訪問數(shù)據(jù)庫的實例詳解

    對python 通過ssh訪問數(shù)據(jù)庫的實例詳解

    今天小編就為大家分享一篇對python 通過ssh訪問數(shù)據(jù)庫的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python hmac模塊使用實例解析

    Python hmac模塊使用實例解析

    這篇文章主要介紹了Python hmac模塊使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)

    python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)

    這篇文章主要介紹了python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng),文中有非常詳細的代碼示例,對正在學習python爬蟲的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04

最新評論