python实现并发和并行的方式有哪些_Python中的并行性和并发性 - Go语言中文社区

python实现并发和并行的方式有哪些_Python中的并行性和并发性


在Python编程语言当中,很多人对Python中的并行性和并发性不了解。今天我们将讨论python中的并发和并行性。在这里,我们将研究Python的多线程,多处理,异步编程,并发和并行性。我们使用python的多处理模块来实现并行性,而Python中的并发是通过线程和异步IO模块来实现的。并行运行的程序将被称为并发,但事实并非如此因此,在不浪费时间的情况下,让我们开始吧。

89896140a2fd908d6d86555d59f6a285.png

平行性

这意味着要同时以相同的顺序执行多个任务。

多重处理:这意味着将任务分配到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的信息,请继续关注中培教育。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_39603327/article/details/114425938
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-14 16:44:06
  • 阅读 ( 634 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢