在Python編程語言當中,很多人對Python中的并行性和并發性不了解。今天我們將討論python中的并發和并行性。在這里,我們將研究Python的多線程,多處理,異步編程,并發和并行性。我們使用python的多處理模塊來實現并行性,而Python中的并發是通過線程和異步IO模塊來實現的。并行運行的程序將被稱為并發,但事實并非如此因此,在不浪費時間的情況下,讓我們開始吧。
平行性
這意味著要同時以相同的順序執行多個任務。
多重處理:這意味著將任務分配到CPU內核上[在終端中鍵入來檢查計算機中的內核數。]。對于任何與CPU綁定的任務,我們可以使用python的multiprocessing模塊。我們只需在多處理中創建一個Pool對象,即可提供一種便捷的方法來跨多個輸入值并行執行函數。讓我們借助一個示例來看看它:
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")
##輸出//
耗時:23.593358993530273秒
耗時:14.405884027481079秒
并發
這意味著同時執行多個任務,但要以重疊或不同或相同的順序進行。(Python在處理并發方面不是很出色),但是它做得相當不錯。
1.多線程:運行不同/多個線程以在單個處理器上執行任務。多線程對于執行IO綁定任務(例如—同時向服務器發送多個請求等)確實非常有用。創建的每個新線程將具有PID(進程ID),并將具有啟動函數。如果要在線程完成其工作后運行loc,可以使用該線程的join()函數。Python與它的GIL有非常復雜的關系,并且代碼的輸出變化很大。
2.異步IO:在Python中,異步IO是單線程單進程設計范例,通過某種方式設法實現并發。
讓我們借助一個示例對其進行研究。
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個數字之和是15.0
耗時:0.0006113052368164062秒
全時耗:5的平方是10.373110294342041 25seconds
5的立方是中耗時:1250.0015938282012939453
5冪2是秒前5個數字的
25
總和是15.0
全時:12.598262786865234秒
以上就是關于Python中的并行性和并發性的全部內容介紹,想了解更多關于Python的信息,請繼續關注中培偉業。