您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在Python中使用 mutiprocessing,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
python的五大特點:1.簡單易學,開發程序時,專注的是解決問題,而不是搞明白語言本身。2.面向對象,與其他主要的語言如C++和Java相比, Python以一種非常強大又簡單的方式實現面向對象編程。3.可移植性,Python程序無需修改就可以在各種平臺上運行。4.解釋性,Python語言寫的程序不需要編譯成二進制代碼,可以直接從源代碼運行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。
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):#定義正在處理進程編號數的函數功能 print 'threading number:%s %s' %(msg,os.getpid())#打印正在處理的進程編號數與對應的系統進程號 time.sleep(2) p = multiprocessing.Pool(processes = 25)#綁定事例,同時執行25個線程 for i in range(100): result.append(p.apply_async(run,('%s' %i,)))#異步傳輸正在運行的進程數字號碼 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中使用 mutiprocessing就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。