目錄
Python并發編程簡介
1.為什么要引入并發編程?
2.有哪些程式提速的方法?
3.python對并發編程的支持
怎樣選擇多執行緒Thread、多行程Process、多協程Coroutine
1.什么是CPU密集型計算、IO密集型計算?
2.多執行緒、多行程、多協程的對比
3.怎樣根據任務選擇對應技術?
Python速度慢的罪魁禍首,全域解釋器鎖GIL
1.python速度慢的兩大原因
2.GIL是什么?
3.為什么有GIL這個東西?
4.怎樣規避GIL帶來的限制?
使用多執行緒,Python多執行緒被加速10倍
1.Python創建多執行緒的方法
2.改寫爬蟲程式,編程多執行緒爬取
3.速度對比:單執行緒爬蟲VS多執行緒爬蟲
Python實作生產者消費者模式多執行緒爬蟲!
1.多組件的Pipeline技術架構
2.生產者消費者爬蟲的架構
3.多執行緒資料通信的queue.Queue
4.代碼撰寫實作生產者消費者爬蟲
Python執行緒安全問題及解決方案
1.執行緒安全概念介紹
2.Lock用于解決執行緒安全問題
3.實體代碼演示問題以及解決方案
好用的執行緒池ThreadPoolExecutor
1.執行緒池的原理
2.使用執行緒池的好處
3.ThreadPoolExecutor的使用語法
4.使用執行緒池改造爬蟲程式
在web服務器中使用執行緒池加速
1.web服務的架構以及特點
2.使用執行緒池ThreadPoolExecutor加速
3.代碼Flask實作web服務并加速實作
使用多行程multiprocessing加速程式的運行
1.有了多執行緒threading,為什么還要用多行程multiprocessing
2.多行程multiprocessing知識梳理
3.代碼實戰:單執行緒、多執行緒、多行程對比CPU密集計算速度
在Flask服務中使用行程池加速
Python異步IO實作并發爬蟲
?在異步IO中使用信號量控制爬蟲并發度
?使用subprocess啟動電腦任意程式聽歌、解壓縮、自動下載等等
Python并發編程簡介
1.為什么要引入并發編程?
場景1:一個網路爬蟲,按順序爬取了1小時,采用并發下載減少到20分鐘!
場景2:一個APP應用,優化前每次打開頁面需要3秒鐘,采用異步并發提升到每次200毫秒;
2.有哪些程式提速的方法?

3.python對并發編程的支持

怎樣選擇多執行緒Thread、多行程Process、多協程Coroutine
1.什么是CPU密集型計算、IO密集型計算?

2.多執行緒、多行程、多協程的對比

3.怎樣根據任務選擇對應技術?

Python速度慢的罪魁禍首,全域解釋器鎖GIL
1.python速度慢的兩大原因

2.GIL是什么?

3.為什么有GIL這個東西?

4.怎樣規避GIL帶來的限制?

使用多執行緒,Python多執行緒被加速10倍
1.Python創建多執行緒的方法

2.改寫爬蟲程式,編程多執行緒爬取
blog_spider.py
import requests
urls=[f"https://www.cnblogs.com/#p{page}"
for page in range(1,50+1)
]
def craw(url):
r=requests.get(url)
print(url,len(r.text))
craw(urls[0])
multi_thread_craw.py
import blog_spider
import threading
import time
def single_thread():
print("single_thread begin")
for url in blog_spider.urls:
blog_spider.craw(url)
print("single_thread end")
def multi_thread():
print("single_thread begin")
threads=[]
for url in blog_spider.urls:
threads.append(
threading.Thread(target=blog_spider.craw,args=(url,))
)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("single_thread end")
if __name__=='__main__':
start=time.time()
single_thread()
end=time.time()
print("single thread cost:",end-start)
start = time.time()
multi_thread()
end = time.time()
print("multi thread cost:", end - start)
3.速度對比:單執行緒爬蟲VS多執行緒爬蟲
Python實作生產者消費者模式多執行緒爬蟲!
1.多組件的Pipeline技術架構

2.生產者消費者爬蟲的架構

3.多執行緒資料通信的queue.Queue

4.代碼撰寫實作生產者消費者爬蟲
Python執行緒安全問題及解決方案
1.執行緒安全概念介紹

2.Lock用于解決執行緒安全問題

3.實體代碼演示問題以及解決方案
好用的執行緒池ThreadPoolExecutor
1.執行緒池的原理

2.使用執行緒池的好處

3.ThreadPoolExecutor的使用語法

4.使用執行緒池改造爬蟲程式
在web服務器中使用執行緒池加速
1.web服務的架構以及特點

2.使用執行緒池ThreadPoolExecutor加速

3.代碼Flask實作web服務并加速實作
import flask
import json
import time
from concurrent.futures import ThreadPoolExecutor
app=flask.Flask(__name__)
pool=ThreadPoolExecutor()
def read_file():
time.sleep(0.1)
return "file result"
def read_db():
time.sleep(0.2)
return "db result"
def read_api():
time.sleep(0.3)
return "api result"
@app.route("/")
def index():
result_file=pool.submit(read_file)
result_db=pool.submit(read_db)
result_api=pool.submit(read_api)
return json.dumps({
"result_file":result_file.result(),
"result_db":result_db.result(),
"result_api":result_api.result(),
})
if __name__=='__main__':
app.run()
使用多行程multiprocessing加速程式的運行
1.有了多執行緒threading,為什么還要用多行程multiprocessing

2.多行程multiprocessing知識梳理

3.代碼實戰:單執行緒、多執行緒、多行程對比CPU密集計算速度

import math
import time
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
PRIMES=[112272535095293]*100
def is_prime(n):
if n<2:
return False
if n==2:
return True
if n%2==0:
return False
sqrt_n=int(math.floor(math.sqrt(n)))
for i in range(3,sqrt_n+1,2):
if n%i==0:
return False
return True
def single_thread():
for number in PRIMES:
is_prime(number)
def multi_thread():
with ThreadPoolExecutor() as pool:
pool.map(is_prime,PRIMES)
def multi_process():
with ProcessPoolExecutor() as pool:
pool.map(is_prime,PRIMES)
if __name__=="__main__":
start=time.time()
single_thread()
end=time.time()
print("single_thread,cost:",end-start,"seconds")
start=time.time()
multi_thread()
end=time.time()
print("multi_thread,cost:",end-start,"seconds")
start=time.time()
multi_process()
end=time.time()
print("multi_process,cost:",end-start,"seconds")
在Flask服務中使用行程池加速
import flask
import math
import json
from concurrent.futures import ProcessPoolExecutor
process_pool=ProcessPoolExecutor()
app=flask.Flask()
def is_prime(n):
if n<2:
return False
if n==2:
return True
if n%2==0:
return False
sqrt_n=int(math.floor(math.sqrt(n)))
for i in range(3,sqrt_n+1,2):
if n%i==0:
return False
return True
@app.route("/is_prime/<numbers>")
def api_is_prime(numbers):
number_list=[int(x) for x in numbers.split(",")]
results=process_pool.map(is_prime,number_list)
return json.dumps(dict(zip(number_list,results)))
if __name__=="__main__":
process_pool=ProcessPoolExecutor()
app.run()
Python異步IO實作并發爬蟲

在異步IO中使用信號量控制爬蟲并發度
使用subprocess啟動電腦任意程式聽歌、解壓縮、自動下載等等


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/342226.html
標籤:python
