跳到主要內容

Python 併發總結,多線程,多進程,異步IO

1 測量函數運行時間
import time 
def profile(func):
def wrapper(*args, **kwargs):
import time
start
= time.time()
func(
*args, **kwargs)
end
= time.time()
print 'COST: {}'.format(end - start)
return wrapper

@profile
def fib(n):
if n<= 2:
return 1
return fib(n-1) + fib(n-2)

fib(
35)

 

2 啟動多個線程,並等待完成   2.1 使用threading.enumerate()
import threading 
for i in range(2):
t
= threading.Thread(target=fib, args=(35,))
t.start()
main_thread
= threading.currentThread()

for t in threading.enumerate():
if t is main_thread:
continue
t.join()

 

2.2 先保存啟動的線程
threads = [] 
for i in range(5):
t
= Thread(target=foo, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()
  3 使用信號量,限制同時能有幾個線程訪問臨界區
from threading import Semaphore 
import time

sema
= Semaphore(3)

def foo(tid):
with sema:
print('{} acquire sema'.format(tid))
wt
= random() * 2
time.sleep(wt)
print('{} release sema'.format(tid))

 

4 鎖,相當於信號量為1的情況
from threading import Thread Lock 
value
= 0
lock
= Lock()
def getlock():
global lock
with lock:
new
= value + 1
time.sleep(
0.001)
value
= new

 

  5 可重入鎖RLock     acquire() 可以不被阻塞的被同一個線程調用多次,release()需要和acquire()調用次數匹配才能釋放鎖 6 條件 Condition 一個線程發出信號,另一個線程等待信號 常用於生產者-消費者模型
import time 
import threading

def consumer(cond):
t
= threading.currentThread()
with cond:
cond.wait()
print("{}: Resource is available to sonsumer".format(t.name))

def producer(cond):
t
= threading.currentThread()
with cond:
print("{}: Making resource available".format(t.name))
cond.notifyAll()

condition
= threading.Condition()
c1
= threading.Thread(name='c1', target=consumer, args=(condition,))
c2
= threading.Thread(name='c2', target=consumer, args=(condition,))
p
= threading.Thread(name='p', target=producer, args=(condition,))

c1.start()
c2.start()
p.start()

 

  7 事件 Event 感覺和Condition 差不多
import time 
import threading
from random import randint

TIMEOUT
= 2

def consumer(event, l):
t
= threading.currentThread()
while 1:
event_is_set
= event.wait(TIMEOUT)
if event_is_set:
try:
integer
= l.pop()
print '{} popped from list by {}'.format(integer, t.name)
event.clear()
# 重置事件狀態
except IndexError: # 為了讓剛啟動時容錯
pass

def producer(event, l):
t
= threading.currentThread()
while 1:
integer
= randint(10, 100)
l.append(integer)
print '{} appended to list by {}'.format(integer, t.name)
event.set()
# 設置事件
time.sleep(1)

event
= threading.Event()
l
= []

threads
= []

for name in ('consumer1', 'consumer2'):
t
= threading.Thread(name=name, target=consumer, args=(event, l))
t.start()
threads.append(t)

p
= threading.Thread(name='producer1', target=producer, args=(event, l))
p.start()
threads.append(p)


for t in threads:
t.join()

 

  8 線程隊列  線程隊列有task_done() 和 join() 標準庫里的例子 往隊列內放結束標誌,注意do_work阻塞可能無法結束,需要用超時
import queue 
def worker():
while True:
item
= q.get()
if item is None:
break
do_work(item)
q.task_done()
q
= queue.Queue()
threads
= []
for i in range(num_worker_threads):
t
= threading.Thread(target=worker)
t.start()
threads.append(t)
for item in source():
q.put(item)
q.join()
for i in range(num_worker_threads):
q.put(None)
for t in threads:
t.join()

 

  9 優先級隊列 PriorityQueue
import threading 
from random import randint
from queue import PriorityQueue

q
= PriorityQueue()

def double(n):
return n * 2

def producer():
count
= 0
while 1:
if count > 5:
break
pri
= randint(0, 100)
print('put :{}'.format(pri))
q.put((pri, double, pri))
# (priority, func, args)
count += 1

def consumer():
while 1:
if q.empty():
break
pri, task, arg
= q.get()
print('[PRI:{}] {} * 2 = {}'.format(pri, arg, task(arg)))
q.task_done()
time.sleep(
0.1)

t
= threading.Thread(target=producer)
t.start()
time.sleep(
1)
t
= threading.Thread(target=consumer)
t.start()

 

  10 線程池 當線程執行相同的任務時用線程池 10.1 multiprocessing.pool 中的線程池
from multiprocessing.pool import ThreadPool 
pool
= ThreadPool(5)
pool.map(
lambda x: x**2, range(5))

 

10.2 multiprocessing.dummy
from multiprocessing.dummy import Pool

 

10.3 concurrent.futures.ThreadPoolExecutor
from concurrent.futures improt ThreadPoolExecutor 
from concurrent.futures import as_completed
import urllib.request

URLS
= ['http://www.baidu.com', 'http://www.hao123.com']

def load_url(url, timeout):
with urllib.request.urlopen(url, timeout
=timeout) as conn:
return conn.read()

with ThreadPoolExecutor(max_workers
=5) as executor:
future_to_url
= {executor.submit(load_url, url, 60): url for url in URLS}
for future in as_completed(future_to_url):
url
= future_to_url[future]
try:
data
= future.result()
execpt Exception as exc:
print("%r generated an exception: %s" % (url, exc))
else:
print("%r page is %d bytes" % (url, len(data)))

 

11 啟動多進程,等待多個進程結束
import multiprocessing 
jobs
= []
for i in range(2):
p
= multiprocessing.Process(target=fib, args=(12,))
p.start()
jobs.append(p)
for p in jobs:
p.join()

 

12 進程池 12.1 multiprocessing.Pool
from multiprocessing import Pool 
pool
= Pool(2)
pool.map(fib, [
36] * 2)

 

  12.2 concurrent.futures.ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor 
import math

PRIMES
= [ 112272535095293, 112582705942171]

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

if __name__ == "__main__":
with ProcessPoolExecutor() as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print("%d is prime: %s" % (number, prime))

 

  13 asyncio   13.1 最基本的示例,單個任務
import asyncio 

async
def hello():
print("Hello world!")
await asyncio.sleep(
1)
print("Hello again")

loop
= asyncio.get_event_loop()
loop.run_until_complete(hello())
loop.close()

 

13.2 最基本的示例,多個任務
import asyncio 

async
def hello():
print("Hello world!")
await asyncio.sleep(
1)
print("Hello again")

loop
= asyncio.get_event_loop()
tasks
= [hello(), hello()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

 

  13.3 結合httpx 執行多個任務並接收返回結果 httpx 接口和 requests基本一致
import asyncio 
import httpx


async
def get_url():
r
= await httpx.get("http://www.baidu.com")
return r.status_code


loop
= asyncio.get_event_loop()
tasks
= [get_url() for i in range(10)]
results
= loop.run_until_complete(asyncio.gather(*tasks))
loop.close()


for num, result in zip(range(10), results):
print(num, result)

 

   本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【精選推薦文章】



如何讓商品強力曝光呢? 網頁設計公司幫您建置最吸引人的網站,提高曝光率!!



想要讓你的商品在網路上成為最夯、最多人討論的話題?



網頁設計公司推薦更多不同的設計風格,搶佔消費者視覺第一線



不管是台北網頁設計公司台中網頁設計公司,全省皆有專員為您服務



想知道最厲害的台北網頁設計公司推薦台中網頁設計公司推薦專業設計師"嚨底家"!!



Orignal From: Python 併發總結,多線程,多進程,異步IO

留言

這個網誌中的熱門文章

韋伯連續劇終於更新 期待第一季順利完結

  地球天文學界的跳票大王詹姆斯·韋伯空間望遠鏡 (James Webb Space Telescope,縮寫為 JWST)自 1996 年以來斷斷續續不按劇本演出的連續劇終於讓焦慮的觀眾們又等到了一次更新:五層遮陽罩測試順利完成。 裝配完成的韋伯望遠鏡與好夥伴遮陽罩同框啦。Credit: NASA   嚴格的測試是任何空間任務順利成功的重中之重。遮陽罩,這個韋伯望遠鏡異常重要的親密夥伴,要是無法正常運轉的話,韋伯的這一季天文界連續劇說不準就要一直拖更了。   詹姆斯·韋伯空間望遠鏡是歷史上造出的最先進的空間望遠鏡。它不僅是一架紅外望遠鏡,還具有特別高的靈敏度。但想要達到辣么高的靈敏度來研究系外行星和遙遠的宇宙童年,韋伯童鞋必須非常"冷靜",體溫升高的話,靈敏度會大大折損。這個時候,遮陽罩就要大顯身手啦。   遮陽罩在韋伯的設計中至關重要。韋伯望遠鏡會被發射到拉格朗日 L2 點,運行軌道很高,遠離太陽、地球與月球。太陽是韋伯的主要熱量干擾的來源,其次是地球與月球。遮陽罩會有效阻斷來自這三大熱源的能量並保護韋伯維持在工作溫度正常運轉。這個工作溫度指的是零下 220 攝氏度(-370 華氏度;50 開爾文)。 上圖中我們可以看出,韋伯望遠鏡的配置大致可分為兩部分:紅色較熱的一面溫度為 85 攝氏度,藍色較冷的一面溫度達到零下 233 攝氏度。紅色的這部分中,儀器包括太陽能板、通信設備、計算機、以及轉向裝置。藍色部分的主要裝置包括鏡面、探測器、濾光片等。Credit: STSci.   遮陽罩的那一部分和望遠鏡的鏡面這部分可以產生非常極端的溫差。遮陽的這面溫度可以達到 110 攝氏度,足以煮熟雞蛋,而背陰處的部分溫度極低,足以凍結氧氣。   工程師們剛剛完成了五層遮陽罩的測試,按照韋伯在 L2 時的運行狀態安裝了遮陽罩。L2 距離地球約 160 萬公里。NASA 表示這些測試使用了航天器的自帶系統來展開遮陽罩,測試目前都已成功完成。韋伯望遠鏡遮陽罩負責人 James Cooper 介紹說這是遮陽罩"第一次在望遠鏡系統的电子設備的控制下展開。儘管這個任務非常艱巨,難度高,但測試順利完成,遮陽罩展開時的狀態非常驚艷"。   遮陽罩由五層 Kapton 製成。Kapton 是一種聚酰亞胺薄膜材料, 耐高溫絕...

LINE 發票管家「一鍵分享發票」新功能,聚餐AA更好算帳

» » LINE 發票管家「一鍵分享發票」新功能,聚餐AA更好算帳 消費明細好清楚,不怕算錯錢啦! by in , 讀取中... 之前介紹過的「LINE 發票管家」,除了能對統一發票、管理消費紀錄,現在還能分享消費明細給其他朋友囉!趕快來看看該如何分享吧!讓大家在聚餐過後,不用再截圖、拍照把消費明細記錄下來分享到 LINE 好友群組,只要用 LINE發票管家就可以隨時一鍵分享消費明細,包括店家、消費時間、消費項目、金額通通都有,聚餐 AA 也更好算帳。 LINE 發票管家「一鍵分享發票」新功能,聚餐AA更好算帳 朋友聚餐完,經常會先由其中一位買單再事後收帳,不過難免擔心忘記拍照紀錄下消費明細,導致算帳變得很麻煩。但是,現在只要用 LINE發票管家,不僅能將發票存入載具後直接匯入發票,進而更方便管理每月的消費情況,現在還能用它來分享消費明細到 LINE 聊天室,不需要截圖,整個流程超級簡單! ▲圖片來源: 當使用 LINE發票管家並且綁定載具後,只要日常消費有將發票存入載具就通通會自動匯入 LINE發票管家。如果想暸解自己近期的消費情況,也能在 LINE發票管家點選「發票明細」查詢所有消費紀錄。 *小提醒:存入手機條碼的發票,待財政部 1-2 日作業時間將發票資料匯入後, 打開單筆消費後分享到指定對象或群組。 像是朋友聚餐或其它購物消費,就能在 LINE發票管家查看每一筆消費的所有消費明細,每一項餐點的項目、價格通通一目了然。如果想將該筆消費內容分享給好友收帳,只要點選該筆消費明細後,接著點選畫面右上角的「分享」按鈕。 網頁設計 最專業,超強功能平台可客製,窩窩以「數位行銷」「品牌經營」「網站與應用程式」「印刷品設計」等四大主軸,為每一位客戶客製建立行銷脈絡及洞燭市場先機,請問 台中電動車 哪裡在賣比較便宜可以到台中景泰電動車門市去看看總店:臺中市潭子區潭秀里雅潭路一段102-1號。 電動車補助 推薦評價好的 iphone維修 中心擁有專業的維修技術團隊,同時聘請資深iphone手機維修專家,現場說明手機問題,快速修理,沒修好不收錢住家的頂樓裝 太陽光電 聽說可發揮隔熱功效一線推薦東陽能源擁有核心技術、...