Python多線程處理實(shí)例詳解【單進(jìn)程/多進(jìn)程】
本文實(shí)例講述了Python多線程處理操作。分享給大家供大家參考,具體如下:
python — 多線程處理
1、一個(gè)進(jìn)程執(zhí)行完后,繼續(xù)下一個(gè)進(jìn)程
root@72132server:~# cd /root/python/multiprocess/ root@72132server:~/python/multiprocess# ls multprocess.py root@72132server:~/python/multiprocess# cat multprocess.py #!/usr/bin/python # --*-- coding:utf-8 --*-- from multiprocessing import Process,Lock#啟用多進(jìn)程,與進(jìn)程鎖 import time,os def sayhi(i): print 'hello world!!!', i time.sleep(10) #lock = Lock() for n in range(100):#執(zhí)行n=100次 p = Process(target=sayhi,args=(n,))#調(diào)用函數(shù)def,若def函數(shù)里面有參數(shù),就是使用args帶值賦值,若函數(shù)沒(méi)有參數(shù)的話就args()為空。 p.start() p.join()#一個(gè)進(jìn)程結(jié)束才會(huì)繼續(xù)下一個(gè)進(jìn)程。如果注釋這句意思是一百個(gè)進(jìn)程同時(shí)發(fā)起 root@72132server:~/python/multiprocess#
運(yùn)行情況:
1)進(jìn)程查看
root@72132server:~# cd /root/python/multiprocess/ root@72132server:~/python/multiprocess# ls multprocess.py root@72132server:~/python/multiprocess# vi multprocess.py root@72132server:~/python/multiprocess# ps -ef | grep multi root 24064 23930 0 20:45 pts/3 00:00:00 grep multi root@72132server:~/python/multiprocess# ps -ef | grep multi root 24066 23930 0 20:45 pts/3 00:00:00 grep multi root@72132server:~/python/multiprocess# ps -ef | grep multi root 24069 23930 0 20:45 pts/3 00:00:00 grep multi root@72132server:~/python/multiprocess# ps -ef | grep multi root 24071 23930 0 20:45 pts/3 00:00:00 grep multi root@72132server:~/python/multiprocess# ps -ef | grep multi root 24073 23930 0 20:46 pts/3 00:00:00 grep multi root@72132server:~/python/multiprocess# ps -ef | grep multi root 24075 23930 0 20:46 pts/3 00:00:00 grep multi root@72132server:~/python/multiprocess#
2)腳本運(yùn)行
root@72132server:~/python/multiprocess# vi multprocess.py root@72132server:~/python/multiprocess# python multprocess.py hello world!!! 0 hello world!!! 1 hello world!!! 2 hello world!!! 3 hello world!!! 4 hello world!!! 5 hello world!!! 6 hello world!!! 7 hello world!!! 8 hello world!!! 9 hello world!!! 10 hello world!!! 11
2、100個(gè)進(jìn)行同時(shí)運(yùn)行
root@72132server:~/python/multiprocess# ls multprocess.py root@72132server:~/python/multiprocess# cat multprocess.py #!/usr/bin/python # --*-- coding:utf-8 --*-- from multiprocessing import Process,Lock#啟用多進(jìn)程,與進(jìn)程鎖 import time,os def sayhi(i): print 'hello world!!!', i time.sleep(10) #lock = Lock() for n in range(100):#執(zhí)行n=100次 p = Process(target=sayhi,args=(n,))#調(diào)用函數(shù)def,若def函數(shù)里面有參數(shù),就是使用args帶值賦值,若函數(shù)沒(méi)有參數(shù)的話就args()為空。 p.start() p.join()#一個(gè)進(jìn)程結(jié)束才會(huì)繼續(xù)下一個(gè)進(jìn)程。如果注釋這句意思是一百個(gè)進(jìn)程同時(shí)發(fā)起 root@72132server:~/python/multiprocess# root@72132server:~/python/multiprocess# vi multprocess.py root@72132server:~/python/multiprocess# cat multprocess.py #!/usr/bin/python # --*-- coding:utf-8 --*-- from multiprocessing import Process,Lock#啟用多進(jìn)程,與進(jìn)程鎖 import time,os def sayhi(i): print 'hello world!!!', i time.sleep(10) #lock = Lock() for n in range(100):#執(zhí)行n=100次 p = Process(target=sayhi,args=(n,))#調(diào)用函數(shù)def,若def函數(shù)里面有參數(shù),就是使用args帶值賦值,若函數(shù)沒(méi)有參數(shù)的話就args()為空。 p.start() #p.join()#一個(gè)進(jìn)程結(jié)束才會(huì)繼續(xù)下一個(gè)進(jìn)程。如果注釋這句意思是一百個(gè)進(jìn)程同時(shí)發(fā)起 root@72132server:~/python/multiprocess#
運(yùn)行情況
1)進(jìn)程查看

2)腳本運(yùn)行(1秒跑完)
root@72132server:~/python/multiprocess# python multprocess.py hello world!!! 0 hello world!!! 2 hello world!!! 3 hello world!!! 5 hello world!!! 7 hello world!!! 8 hello world!!! 6 hello world!!! 9 hello world!!! 10 hello world!!! 11 hello world!!! 14 hello world!!! 4 hello world!!! 15 hello world!!! 16 hello world!!! 1 hello world!!! 13 hello world!!! 18 hello world!!! 20 hello world!!! 19 hello world!!! 21 hello world!!! 12 hello world!!! 17 hello world!!! 23 hello world!!! 24 hello world!!! 26 hello world!!! 27 hello world!!! 22 hello world!!! 29 hello world!!! 31 hello world!!! 32 hello world!!! 33 hello world!!! 34 hello world!!! 28 hello world!!! 25 hello world!!! 30 hello world!!! 35 hello world!!! 36 hello world!!! 39 hello world!!! 41 hello world!!! 37 hello world!!! 40 hello world!!! 42 hello world!!! 43 hello world!!! 46 hello world!!! 47 hello world!!! 48 hello world!!! 38 hello world!!! 44 hello world!!! 45 hello world!!! 50 hello world!!! 51 hello world!!! 53 hello world!!! 54 hello world!!! 55 hello world!!! 57 hello world!!! 49 hello world!!! 58 hello world!!! 59 hello world!!! 60 hello world!!! 61 hello world!!! 62 hello world!!! 63 hello world!!! 64 hello world!!! 65 hello world!!! 66 hello world!!! 67 hello world!!! 68 hello world!!! 69 hello world!!! 56 hello world!!! 70 hello world!!! 52 hello world!!! 71 hello world!!! 72 hello world!!! 73 hello world!!! 76 hello world!!! 74 hello world!!! 78 hello world!!! 79 hello world!!! 80 hello world!!! 82 hello world!!! 77 hello world!!! 83 hello world!!! 84 hello world!!! 85 hello world!!! 86 hello world!!! 87 hello world!!! 81 hello world!!! 91 hello world!!! 75 hello world!!! 89 hello world!!! 92 hello world!!! 88 hello world!!! 90 hello world!!! 93 hello world!!! 95 hello world!!! 94 hello world!!! 96 hello world!!! 98 hello world!!! 9
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)入門(mén)教程》及《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python 多進(jìn)程和多線程使用詳解
- Python全局鎖中如何合理運(yùn)用多線程(多進(jìn)程)
- python線程安全及多進(jìn)程多線程實(shí)現(xiàn)方法詳解
- Python實(shí)現(xiàn)多線程/多進(jìn)程的TCP服務(wù)器
- python多線程與多進(jìn)程及其區(qū)別詳解
- Python實(shí)現(xiàn)的服務(wù)器示例小結(jié)【單進(jìn)程、多進(jìn)程、多線程、非阻塞式】
- Python中單線程、多線程和多進(jìn)程的效率對(duì)比實(shí)驗(yàn)實(shí)例
- Python并發(fā):多線程與多進(jìn)程的詳解
- Python實(shí)現(xiàn)的多進(jìn)程和多線程功能示例
- Python多線程與多進(jìn)程相關(guān)知識(shí)總結(jié)
相關(guān)文章
Python中字節(jié)數(shù)組和16進(jìn)制字符串轉(zhuǎn)換方式
這篇文章主要介紹了Python中字節(jié)數(shù)組和16進(jìn)制字符串轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
使用python實(shí)現(xiàn)簡(jiǎn)單爬取網(wǎng)頁(yè)數(shù)據(jù)并導(dǎo)入MySQL中的數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了如何使用 python 實(shí)現(xiàn)簡(jiǎn)單爬取網(wǎng)頁(yè)數(shù)據(jù)并導(dǎo)入 MySQL 中的數(shù)據(jù)庫(kù),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-06-06
Python判斷以什么結(jié)尾以什么開(kāi)頭的實(shí)例
今天小編就為大家分享一篇Python判斷以什么結(jié)尾以什么開(kāi)頭的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
講解如何利用 Python完成 Saga 分布式事務(wù)
這篇文章主要介紹了如何利用 Python 完成一個(gè) Saga 的分布式事務(wù),需要的朋友可以參考下面文章具體的內(nèi)容2021-09-09
python使用pika庫(kù)調(diào)用rabbitmq參數(shù)使用詳情
這篇文章主要介紹了python使用pika庫(kù)調(diào)用rabbitmq參數(shù)使用詳情,文章通過(guò)展開(kāi)文章主題分享了三種方式,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
淺談OpenCV中的新函數(shù)connectedComponentsWithStats用法
這篇文章主要介紹了淺談OpenCV中的新函數(shù)connectedComponentsWithStats用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Python小紅書(shū)旋轉(zhuǎn)驗(yàn)證碼識(shí)別實(shí)戰(zhàn)教程
這篇文章主要介紹了Python小紅書(shū)旋轉(zhuǎn)驗(yàn)證碼識(shí)別實(shí)戰(zhàn)教程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-08-08
基于Python實(shí)現(xiàn)自動(dòng)用小寫(xiě)字母替換文件后綴的大寫(xiě)字母
本文介紹基于Python語(yǔ)言,基于一個(gè)大文件夾,遍歷其中的多個(gè)子文件夾,對(duì)于每一個(gè)子文件夾中的大量文件,批量將其文件的名稱(chēng)或后綴名中的字母由大寫(xiě)修改為小寫(xiě)的方法,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04

