Python多cpu并行编程是怎样,要点有什么
Admin 2022-08-31 群英技术资讯 1143 次浏览
这篇文章给大家分享的是“Python多cpu并行编程是怎样,要点有什么”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。pip install pp
#-*- coding: UTF-8 -*-
import math, sys, time
import pp
def IsPrime(n):
"""返回n是否是素数"""
if not isinstance(n, int):
raise TypeError("argument passed to is_prime is not of 'int' type")
if n < 2:
return False
if n == 2:
return True
max = int(math.ceil(math.sqrt(n)))
i = 2
while i <= max:
if n % i == 0:
return False
i += 1
return True
def SumPrimes(n):
for i in xrange(15):
sum([x for x in xrange(2,n) if IsPrime(x)])
"""计算从2-n之间的所有素数之和"""
return sum([x for x in xrange(2,n) if IsPrime(x)])
inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
# start_time = time.time()
# for input in inputs:
# print SumPrimes(input)
# print '单线程执行,总耗时', time.time() - start_time, 's'
# # tuple of all parallel python servers to connect with
ppservers = ()
#ppservers = ("10.0.0.1",)
if len(sys.argv) > 1:
ncpus = int(sys.argv[1])
# Creates jobserver with ncpus workers
job_server = pp.Server(ncpus, ppservers=ppservers)
else:
# Creates jobserver with automatically detected number of workers
job_server = pp.Server(ppservers=ppservers)
print "pp 可以用的工作核心线程数", job_server.get_ncpus(), "workers"
start_time = time.time()
jobs = [(input, job_server.submit(SumPrimes,(input,), (IsPrime,), ("math",))) for input in inputs]#submit提交任务
for input, job in jobs:
print "Sum of primes below", input, "is", job()# job()获取方法执行结果
print "多线程下执行耗时: ", time.time() - start_time, "s"
job_server.print_stats()#输出执行信息
执行结果
pp 可以用的工作核心线程数 4 workers
Sum of primes below 100000 is 454396537
Sum of primes below 100100 is 454996777
Sum of primes below 100200 is 455898156
Sum of primes below 100300 is 456700218
Sum of primes below 100400 is 457603451
Sum of primes below 100500 is 458407033
Sum of primes below 100600 is 459412387
Sum of primes below 100700 is 460217613
多线程下执行耗时: 15.4971420765 s
Job execution statistics:
job count | % of all jobs | job time sum | time per job | job server
8 | 100.00 | 60.9828 | 7.622844 | local
Time elapsed since server creation 15.4972219467
0 active tasks, 4 cores
def submit(self, func, args=(), depfuncs=(), modules=(),
callback=None, callbackargs=(), group='default', globals=None):
"""Submits function to the execution queue
func - function to be executed 执行的方法
args - tuple with arguments of the 'func' 方法传入的参数,使用元组
depfuncs - tuple with functions which might be called from 'func' 传入自己写的方法 ,元组
modules - tuple with module names to import 传入 模块
callback - callback function which will be called with argument
list equal to callbackargs+(result,)
as soon as calculation is done
callbackargs - additional arguments for callback function
group - job group, is used when wait(group) is called to wait for
jobs in a given group to finish
globals - dictionary from which all modules, functions and classes
will be imported, for instance: globals=globals()
"""
# 多进程
import multiprocessing as mp
def job(q, a, b):
print('aaa')
q.put(a**1000+b*1000) # 把计算结果放到队列
# 多进程
if __name__ == '__main__':
q = mp.Queue() # 一个队列
p1 = mp.Process(target=job, args=(q, 100, 200))
p2 = mp.Process(target=job, args=(q, 100, 200))
p1.start()
p1.join()
# print(p1.ident)
p2.start()
p2.join()
res = q.get() + q.get() # 读取队列,这里面保存了两次计算得到的结果
print('result:', res)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
jieba库是一款优秀的Python第三方中文分词库,jieba支持三种分词模式:精确模式、全模式和搜索引擎模式,下面这篇文章主要给大家介绍了关于Python第三方库jieba库与中文分词的相关资料,需要的朋友可以参考下
怎么用Python做中文词云?一些朋友比较好奇中文词云的实现,因此这篇文章就主要给大家介绍Python实现中文词云的方法,下文的示例代码对大家了解Python实现词云的方法有一定的帮助,感兴趣的朋友接下来一起跟随小编来学习一下吧。
这篇文章给大家分享的是关于CNN的内容,CNN也就是卷积神经网络,那么CNN到底有什么用呢?可以解决什么问题呢?CNN的权值共享网络结构使之更类似于生物神经网络,降低了网络模型的复杂度,减少了权值的数量。接下来小编就给大家来详细的介绍一下CNN的基础内容,感兴趣的朋友就继续往下看吧。
这篇文章主要介绍了python 列表生成式的相关资料,帮助大家更好的理解和学习python开发,感兴趣的朋友可以了解下
这篇文章主要给大家介绍了关于利用Python将彩色图像转为灰度图像的两种方法,以及python 批量将图片转为灰度图的方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008