性少妇vide0seⅹfree_国产剧情视频在线观看_日日碰夜夜爽_九九这里只有精品视频_性free毛茸茸偷窥videos_国产v亚洲

中培偉業IT資訊頻道
您現在的位置:首頁 > IT資訊 > 軟件研發 > Python中的并行性和并發性

Python中的并行性和并發性

2020-08-21 17:40:35 | 來源:中培企業IT培訓網

在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的信息,請繼續關注中培偉業。

相關閱讀

主站蜘蛛池模板: 亚洲毛片欧洲毛片国产一品色 | 惊弦60集全免费大牛 | 亚洲经典国产欧美 | 大地中文在线观看免费高清 | 欧美亚洲另类丝袜综合网 | 未禁18成禁人免费无遮挡 | 制服丝袜电影在线观看 | 欧美亚洲国产激情 | 亚洲涩婷婷| 日本超碰在线观看 | 国产第7页 | 欧美精品成人一区二区在线 | 久草国产在线视频 | 青娱乐国产在线视频 | 婷婷伊人综合亚洲综合网 | 高清国语自产拍免费一区二区三区 | 天天天在线综合网 | 亚洲制服一区 | 一级全黄60分钟免费 | 亚洲人成网站在线观看妞妞网 | 交换系列100篇护士 97快播 | 白人と日本人の交わりビデオ | 中国按摩xxxx做受a | 看大片免费网站 | 免费看国产黄色 | 欧美日韩中文国产一区发布 | 一级电影在线观看 | 亚洲日韩AV无码不卡一区二区三区 | 日日噜噜噜噜人人爽日本精品 | 国产精品无码专区第一页 | 免费观看的av网站 | 精品亚洲区| 班花在教室伦流澡到高潮视频 | 啊灬嗯灬啊灬用力点日出 | 欧美成人黄色网 | 96国产精品视频 | 在线观看美女黄视频 | 三级网址电影 | 男人女人免费看视频5分钟 sis001亚洲原创区 | 中文字幕人妻互换AV久久 | 狠狠擦狠狠操 |