"""
Created on Thu Oct 19 11:11:45 2017@author: Administrator
"""#-*- coding: UTF-8 -*-import math, sys, time
import pp
defIsPrime(n):"""返回n是否是素數(shù)"""ifnot isinstance(n, int):raise TypeError("argument passed to is_prime is not of 'int' type")if n < 2:returnFalseif n == 2:""returnTruemax = int(math.ceil(math.sqrt(n)))i = 2while i <= max:if n % i == 0:returnFalsei += 1returnTruedefSumPrimes(n):for i in range(15):sum([x for x in range(2,n) if IsPrime(x)])"""計算從2-n之間的所有素數(shù)之和"""return sum([x for x in range(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í)行,總耗時', 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 workersjob_server = pp.Server(ncpus, ppservers=ppservers)
else:# Creates jobserver with automatically detected number of workersjob_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]for input, job in jobs:(input,job())#print ("Sum of primes below", input, "is", job())print ("多線程下執(zhí)行耗時: ", time.time() - start_time, "s")job_server.print_stats()
pp 可以用的工作核心線程數(shù) 4 workers
多線程下執(zhí)行耗時: 23.168389320373535 s
Job execution statistics:job count | % of all jobs | job time sum | time per job | job server8 | 100.00 | 89.4352 | 11.179397 | local
Time elapsed since server creation 23.1693892478942870 active tasks, 4 cores
jobs = [(input, job_server.submit(save_to_mongo,(ct,rl,input,20170501), function, stock)) for input in inputs] 運行方式: for input, job in jobs: (input,job())
import math, sys, time
import ppinputs = plg
'''start_time = time.time()
for input in inputs:
print ( SumPrimes(input))
print ('單線程執(zhí)行,總耗時', time.time() - start_time, 's')
'''
# tuple ofall 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()<font color='grape' size=4.5>#主函數(shù)save_to_mongo需要調(diào)用的函數(shù)庫
function=(common_rent_price,add_block,add_room,take_location,some_district_information,neighbor,most_frecuncy_rent,mean_rent_price,no_source_price,block_price,block_rule,RENT_no_data_mostblock_price,region_district_list,add_rent_type,)stock=("os","collections","pymongo","numpy","pandas",)
jobs = [(input, job_server.submit(save_to_mongo,(ct,rl,input,20170501), function, stock)) for input in inputs]for input, job in jobs:(input,job())print ("Sum of primes below", input, "is", job())#print('index=',disname.index(input))
print ("多線程下執(zhí)行耗時: ", time.time() - start_time, "s")job_server.print_stats()