python多線程與多進程--存活主機ping掃描以及爬取股票價格


python多線程與多進程

多線程:

案例:掃描給定網絡中存活的主機(通過ping來測試,有響應則說明主機存活)

普通版本:

#掃描給定網絡中存活的主機(通過ping來測試,有響應則說明主機存活)
import
sys import subprocess import time def ping(net,start=100,end=200,n=2,w=5): for i in range(start,end+1): ip=net+"."+str(i) command="ping %s -n %d -w %d"%(ip,n,w) print(ip,("","不通")[subprocess.call(command,stdout=open("nul","w"))]) #stdout=open("nul","w") #不顯示命令執行返回的結果 t1=time.time() if len(sys.argv)!=2: print("參數輸入錯誤!") print("運行示例:") print("test01.py 123.125.114") elif len(sys.argv)==2: net=sys.argv[1] ping(net) t2=time.time() print("程序耗時%f秒!"%(t2-t1)) #195.091611秒

運行效果如下:

在python里面,線程的創建有兩種方式,其一使用Thread類創建
導入Python標准庫中的Thread模塊
from threading import Thread 
創建一個線程
mthread = threading.Thread(target=function_name, args=(function_parameter1, function_parameterN))
 啟動剛剛創建的線程
mthread .start()
function_name: 需要線程去執行的方法名
args: 線程執行方法接收的參數,該屬性是一個元組,如果只有一個參數也需要在末尾加逗號。

多線程版:

import sys import subprocess import time from threading import Thread #在python里面,線程的創建有兩種方式,其一使用Thread類創建 # 導入Python標准庫中的Thread模塊  #from threading import Thread # # 創建一個線程  #mthread = threading.Thread(target=function_name, args=(function_parameter1, function_parameterN))  # 啟動剛剛創建的線程  #mthread .start() #function_name: 需要線程去執行的方法名 #args: 線程執行方法接收的參數,該屬性是一個元組,如果只有一個參數也需要在末尾加逗號。
 result=[] def ping1(ip): command="ping %s -n 1 -w 20"%(ip) result.append([ip,subprocess.call(command)]) def ping(net,start=100,end=200): for i in range(start,end+1): ip=net+"."+str(i) th=Thread(target=ping1,args=(ip,)) th.start() def main(): if len(sys.argv)!=2: print("參數輸入錯誤!") print("運行示例:") print("test01.py 123.125.114") elif len(sys.argv)==2: net=sys.argv[1] ping(net) if __name__=='__main__': t1=time.time() main() while len(result)!=101: time.sleep(1) print(result) t2=time.time() print("程序耗時%f秒!"%(t2-t1))   #1.585263秒

多線程案例2:爬取股票的價格

多線程 #爬取股票的價格
import requests import re import time from threading import Thread code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377] m1=re.compile(r"price: '(\d{1,3}\.\d{2}')") def getprice(id): url="http://quotes.money.163.com/0%s.html"%id txt=requests.get(url).text price=m1.search(txt).group(1) print(id,price) if __name__=="__main__": ts=[] start=time.time() for id in code: t=Thread(target=getprice,args=(id,)) ts.append(t) t.start() for t in ts: t.join() #等待子線程運行完,主線程再運行
    print("程序耗時:",time.time()-start)     

多進程:

爬取股票的價格(多進程版)

#多進程 #爬取股票的價格
import requests import re import time from multiprocessing import Process from threading import Thread code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377] m1=re.compile(r"price: '(\d{1,3}\.\d{2}')") def getprice(id): url="http://quotes.money.163.com/0%s.html"%id txt=requests.get(url).text price=m1.search(txt).group(1) print(id,price) ps=[]   #進程池 
if __name__=="__main__": start=time.time() for id in code: p=Process(target=getprice,args=(id,)) ps.append(p) #把進程放入列表(進程池)
        p.start()   #啟動進程
    for p in ps: p.join() print(time.time()-start)  

爬取股票的價格(多進程版)帶Pool

#爬取股票的價格
import requests import re import time from multiprocessing import Pool #多進程帶Pool
 code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377] m1=re.compile(r"price: '(\d{1,3}\.\d{2}')") def getprice(id): url="http://quotes.money.163.com/0%s.html"%id txt=requests.get(url).text price=m1.search(txt).group(1) print(id,price) if __name__=="__main__": start=time.time() p=Pool(4) for id in code: p.apply_async(getprice,args=(id,))  #async異步,第一個參數是函數名,第二個是此函數的參數
 p.close() p.join() #等待子線程運行完,主線程再運行
    print("程序耗時:",time.time()-start) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM