在Python編程語(yǔ)言當(dāng)中,很多人對(duì)Python中的并行性和并發(fā)性不了解。今天我們將討論python中的并發(fā)和并行性。在這里,我們將研究Python的多線程,多處理,異步編程,并發(fā)和并行性。我們使用python的多處理模塊來(lái)實(shí)現(xiàn)并行性,而Python中的并發(fā)是通過(guò)線程和異步IO模塊來(lái)實(shí)現(xiàn)的。并行運(yùn)行的程序?qū)⒈环Q為并發(fā),但事實(shí)并非如此因此,在不浪費(fèi)時(shí)間的情況下,讓我們開始吧。
平行性
這意味著要同時(shí)以相同的順序執(zhí)行多個(gè)任務(wù)。
多重處理:這意味著將任務(wù)分配到CPU內(nèi)核上[在終端中鍵入來(lái)檢查計(jì)算機(jī)中的內(nèi)核數(shù)。]。對(duì)于任何與CPU綁定的任務(wù),我們可以使用python的multiprocessing模塊。我們只需在多處理中創(chuàng)建一個(gè)Pool對(duì)象,即可提供一種便捷的方法來(lái)跨多個(gè)輸入值并行執(zhí)行函數(shù)。讓我們借助一個(gè)示例來(lái)看看它:
import multiprocessingimport os import time import numpy as npdef DotProduct(A):
dot_product = np.dot(A[0],A[1])
return
List = [[np.arange(1000000).reshape(5000,200),np.arange(1000000).reshape(200,5000)],
[np.arange(1000000).reshape(500,2000),np.arange(1000000).reshape(2000,500)],
[np.arange(1000000).reshape(5000,200),np.arange(1000000).reshape(200,5000)]]
if __name__ == "__main__":
# executing a code without multiprocessing .. ie. on single core .
start = time.time()
B = list(map(DotProduct,List))
end = time.time() - start
print("Full time taken : " , end , "seconds")
# lets look at executing same code with multiprocesing module on multiple cores ..
start = time.time()
pool = multiprocessing.cpu_count()
with multiprocessing.Pool(pool) as p:
print(p.map(DotProduct,List))
end = time.time() - start
print("Full time taken : " , end , "seconds")
##輸出//
耗時(shí):23.593358993530273秒
耗時(shí):14.405884027481079秒
并發(fā)
這意味著同時(shí)執(zhí)行多個(gè)任務(wù),但要以重疊或不同或相同的順序進(jìn)行。(Python在處理并發(fā)方面不是很出色),但是它做得相當(dāng)不錯(cuò)。
1.多線程:運(yùn)行不同/多個(gè)線程以在單個(gè)處理器上執(zhí)行任務(wù)。多線程對(duì)于執(zhí)行IO綁定任務(wù)(例如—同時(shí)向服務(wù)器發(fā)送多個(gè)請(qǐng)求等)確實(shí)非常有用。創(chuàng)建的每個(gè)新線程將具有PID(進(jìn)程ID),并將具有啟動(dòng)函數(shù)。如果要在線程完成其工作后運(yùn)行l(wèi)oc,可以使用該線程的join()函數(shù)。Python與它的GIL有非常復(fù)雜的關(guān)系,并且代碼的輸出變化很大。
2.異步IO:在Python中,異步IO是單線程單進(jìn)程設(shè)計(jì)范例,通過(guò)某種方式設(shè)法實(shí)現(xiàn)并發(fā)。
讓我們借助一個(gè)示例對(duì)其進(jìn)行研究。
import threadingimport os import time import numpy as npdef BasicOperation():
# square of number
def square(number):
return number*number
# cube of a number
def cube(number):
return number**3
# nth power of a number
def nth_power(number,power):
return number**power
# sum of n numbers
def sum_of_n_numbers(number):
return number*(number+1)/2
# using functions to drive a program ...
print("square of 5 is " , square(5))
print("cube of 5 is " , cube(5))
print("5 raise to power 2 is " , nth_power(5,2))
print("sum of first 5 numbers is" , sum_of_n_numbers(5))
def DotProduct():
A = np.arange(1000000).reshape(5000,200)
B = np.arange(1000000).reshape(200,5000)
Dot = np.dot(A,B)if __name__ == "__main__":
# without threading ...
start = time.time()
BasicOperation()
Mid = time.time() - start
print("Mid time taken : " , Mid , "seconds")
DotProduct()
end = time.time() - start
print("Full time taken : " , end , "seconds")
# with threading ...
start = time.time()
Thread_1 = threading.Thread(target = BasicOperation, name = ' Basic Operation Thread ')
Thread_2 = threading.Thread(target = DotProduct , name=' Dot Product Thread ')
Thread_1.start()
Thread_2.start()
Thread_1.join()
Mid = time.time() - start
print("Mid time taken : " , Mid , "seconds")
Thread_2.join()
end = time.time() - start
print("Full time taken : " , end , "seconds")
##輸出//
5的平方是25
的5的立方是125
5 升到冪2是25
的前5個(gè)數(shù)字之和是15.0
耗時(shí):0.0006113052368164062秒
全時(shí)耗:5的平方是10.373110294342041 25seconds
5的立方是中耗時(shí):1250.0015938282012939453
5冪2是秒前5個(gè)數(shù)字的
25
總和是15.0
全時(shí):12.598262786865234秒
以上就是關(guān)于Python中的并行性和并發(fā)性的全部?jī)?nèi)容介紹,想了解更多關(guān)于Python的信息,請(qǐng)繼續(xù)關(guān)注中培偉業(yè)。