網(wǎng)站首頁 編程語言 正文
多cpu并行編程
- python多線程只能算并發(fā),因?yàn)樗悄苁褂靡粋€(gè)cpu內(nèi)核
- python下pp包支持多cpu并行計(jì)算
安裝
pip install pp
使用
#-*- coding: UTF-8 -*-
import math, sys, time
import pp
def IsPrime(n):
"""返回n是否是素?cái)?shù)"""
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)])
"""計(jì)算從2-n之間的所有素?cái)?shù)之和"""
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 '單線程執(zhí)行,總耗時(shí)', 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 可以用的工作核心線程數(shù)", job_server.get_ncpus(), "workers"
start_time = time.time()
jobs = [(input, job_server.submit(SumPrimes,(input,), (IsPrime,), ("math",))) for input in inputs]#submit提交任務(wù)
for input, job in jobs:
print "Sum of primes below", input, "is", job()# job()獲取方法執(zhí)行結(jié)果
print "多線程下執(zhí)行耗時(shí): ", time.time() - start_time, "s"
job_server.print_stats()#輸出執(zhí)行信息
執(zhí)行結(jié)果
pp 可以用的工作核心線程數(shù) 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
多線程下執(zhí)行耗時(shí): ?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
submit 函數(shù)定義
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 執(zhí)行的方法
args - tuple with arguments of the 'func' 方法傳入的參數(shù),使用元組
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()
"""
多核cpu并行計(jì)算
- 多進(jìn)程實(shí)現(xiàn)并行計(jì)算的簡(jiǎn)單示例
- 這里我們開兩個(gè)進(jìn)程,計(jì)算任務(wù)也簡(jiǎn)潔明了
# 多進(jìn)程
import multiprocessing as mp
def job(q, a, b):
print('aaa')
q.put(a**1000+b*1000) # 把計(jì)算結(jié)果放到隊(duì)列
# 多進(jìn)程
if __name__ == '__main__':
q = mp.Queue() # 一個(gè)隊(duì)列
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() # 讀取隊(duì)列,這里面保存了兩次計(jì)算得到的結(jié)果
print('result:', res)
原文鏈接:https://liuhuiyao.blog.csdn.net/article/details/72853750
相關(guān)推薦
- 2022-10-26 如何查看git分支從哪個(gè)源分支拉的_相關(guān)技巧
- 2022-06-01 Androidstudio調(diào)用攝像頭拍照并保存照片_Android
- 2022-10-19 react實(shí)現(xiàn)動(dòng)態(tài)選擇框_React
- 2022-05-17 ribbon和nacos獲取服務(wù)列表不一致問題
- 2022-04-28 WPF使用DockPanel停靠面板布局_實(shí)用技巧
- 2022-04-05 關(guān)于Unity中RectTransform與transform的區(qū)別_C#教程
- 2022-12-06 React-Hook中使用useEffect清除定時(shí)器的實(shí)現(xiàn)方法_React
- 2023-10-15 小程序動(dòng)態(tài)隱藏分享按鈕
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支