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

Python多線程處理實例詳解【單進程/多進程】

 更新時間:2019年01月30日 12:01:46   作者:徐為波  
這篇文章主要介紹了Python多線程處理,結(jié)合實例形式總結(jié)分析了Python單進程、多進程、多線程等相關(guān)操作技巧與注意事項,需要的朋友可以參考下

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

python — 多線程處理

1、一個進程執(zhí)行完后,繼續(xù)下一個進程

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#啟用多進程,與進程鎖
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ù)沒有參數(shù)的話就args()為空。
  p.start()
  p.join()#一個進程結(jié)束才會繼續(xù)下一個進程。如果注釋這句意思是一百個進程同時發(fā)起
root@72132server:~/python/multiprocess#

運行情況:

1)進程查看

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)腳本運行

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個進行同時運行

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#啟用多進程,與進程鎖
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ù)沒有參數(shù)的話就args()為空。
  p.start()
  p.join()#一個進程結(jié)束才會繼續(xù)下一個進程。如果注釋這句意思是一百個進程同時發(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#啟用多進程,與進程鎖
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ù)沒有參數(shù)的話就args()為空。
  p.start()
  #p.join()#一個進程結(jié)束才會繼續(xù)下一個進程。如果注釋這句意思是一百個進程同時發(fā)起
root@72132server:~/python/multiprocess#

運行情況

1)進程查看

2)腳本運行(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)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

  • django用戶登錄驗證的完整示例代碼

    django用戶登錄驗證的完整示例代碼

    這篇文章主要給大家介紹了關(guān)于django用戶登錄驗證的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring django具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • 基于pandas數(shù)據(jù)清洗的實現(xiàn)示例

    基于pandas數(shù)據(jù)清洗的實現(xiàn)示例

    數(shù)據(jù)清洗是數(shù)據(jù)科學和數(shù)據(jù)分析中非常重要的一個步驟,本文主要介紹了基于pandas的數(shù)據(jù)清洗,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • Pandas 合并多個Dataframe(merge,concat)的方法

    Pandas 合并多個Dataframe(merge,concat)的方法

    今天小編就為大家分享一篇Pandas 合并多個Dataframe(merge,concat)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • TensorFlow平臺下Python實現(xiàn)神經(jīng)網(wǎng)絡

    TensorFlow平臺下Python實現(xiàn)神經(jīng)網(wǎng)絡

    這篇文章主要為大家詳細介紹了TensorFlow平臺下Python實現(xiàn)神經(jīng)網(wǎng)絡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • python 多線程爬取壁紙網(wǎng)站的示例

    python 多線程爬取壁紙網(wǎng)站的示例

    這篇文章主要介紹了python 多線程爬取壁紙網(wǎng)站的示例,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-02-02
  • 使用Python處理KNN分類算法的實現(xiàn)代碼

    使用Python處理KNN分類算法的實現(xiàn)代碼

    KNN分類算法(K-Nearest-Neighbors?Classification),又叫K近鄰算法,是一個概念極其簡單,而分類效果又很優(yōu)秀的分類算法,這篇文章主要介紹了使用Python處理KNN分類算法,需要的朋友可以參考下
    2022-09-09
  • Pytorch中的廣播機制詳解(Broadcast)

    Pytorch中的廣播機制詳解(Broadcast)

    這篇文章主要介紹了Pytorch中的廣播機制詳解(Broadcast),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python求列表對應元素的乘積和的實現(xiàn)

    python求列表對應元素的乘積和的實現(xiàn)

    這篇文章主要介紹了python求列表對應元素的乘積和的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • python實現(xiàn)Adapter模式實例代碼

    python實現(xiàn)Adapter模式實例代碼

    這篇文章主要介紹了python實現(xiàn)Adapter模式實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Flask-Mail用法實例分析

    Flask-Mail用法實例分析

    這篇文章主要介紹了Flask-Mail用法,結(jié)合實例形式分析了Flask-Mail的安裝、配置參數(shù)及簡單使用技巧,需要的朋友可以參考下
    2018-07-07

最新評論