博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
计算型密集,多进程执行
阅读量:6937 次
发布时间:2019-06-27

本文共 3148 字,大约阅读时间需要 10 分钟。

  hot3.png

"""计算型密集,多进程执行# 自定义个计算型密集任务,去完成测试普通执行,和多进程执行的区别get_nowait(),可以判断队列是否为空math模块和python内置的模块,都存在有pow()函数,pow(x,y)等价与x**y   例如pow(2,3)  内置输出8,math模块输出8.0multiprocessing.Queue()和queue.Queue() 不一样,当使用多进程时,使用multiprocessingcountlist=[1,5,8,9]count =2count += 1 if count in countlist else 0写文件时,循环放在打开文件里面,可以减少文件的打开次数"""import threadingimport timeimport multiprocessingimport logginglogging.basicConfig(filename="",level=logging.INFO)new_queue = multiprocessing.Queue()def init_queue():    for abum in range(10):        new_queue.put(abum)    return new_queueclass BasicFunction(object):    def __init__(self):        self.q = init_queue()    def task_cpu(self):        pass    def run(self):        c = self.task_cpu()        logging.info(f"单个进程计算所用时间为{c}")class OneProcess(BasicFunction):    def __init__(self):        super().__init__()    def task_cpu(self):        c_list = list(range(10000))        startTime = time.time()        while self.q.qsize():            global count            count = 100            for num in range(10000):                count += pow(3*3,3*3) if num in c_list else 0            data = self.q.get(timeout=1)        endTime = time.time()        return endTime-startTimeclass MoreProcess(BasicFunction):    def __init__(self):        super().__init__()    def task_cpu(self):        c_list = list(range(10000))        while self.q.qsize():            global count            count = 100            for num in range(10000):                count += pow(3 * 3, 3 * 3) if num in c_list else 0            if self.q.qsize():                self.q.get(timeout=1)    def calculation_time(self):        processsing_list = []        startTime = time.time()        for i in range(multiprocessing.cpu_count()):            task = multiprocessing.Process(target=self.task_cpu)            processsing_list.append(task)        for pro in processsing_list:            pro.start()        for pro in processsing_list:            if pro.is_alive():                pro.join()        endTime = time.time()        return endTime-startTime    def run(self):        s = self.calculation_time()        logging.info(f"多个进程计算所用时间为{s}")class Morethread(BasicFunction):    def __init__(self):        super().__init__()    def task_cpu(self):        c_list = list(range(10000))        while self.q.qsize():            global count            count = 100            for num in range(10000):                count += pow(3 * 3, 3 * 3) if num in c_list else 0            if self.q.qsize():                self.q.get(timeout=1)    def calcul_time(self):        threads = []        threadnum = 4        startTime = time.time()        for i in range(0,threadnum):            threads.append(threading.Thread(target=self.task_cpu))        for thre in threads:            thre.start()        for thre in threads:            thre.join()        endTime = time.time()        return endTime - startTime    def run(self):        ctime = self.calcul_time()        logging.info(f"四个线程计算所用时间为{ctime}")if __name__ == '__main__':    op = OneProcess()    op.run()    mp = MoreProcess()    mp.run()    mt = Morethread()    mt.run()

转载于:https://my.oschina.net/mypeng/blog/3007800

你可能感兴趣的文章
My Spring Boot Samples
查看>>
1834. [ZJOI2010]网络扩容【费用流】
查看>>
quarz时间配置
查看>>
kernel 进阶API
查看>>
PAT 之 A+B和C
查看>>
MacBook Touch Bar 使用技巧
查看>>
C标准库-数值字符串转换与内存分配函数
查看>>
ABAP中Conversion Routine示例
查看>>
Git基本操作
查看>>
AtCoder Regular Contest 103 E Tr/ee
查看>>
《失业的程序员》语录(一)
查看>>
[BZOJ 3143][Hnoi2013]游走(高斯消元+期望)
查看>>
03-高级选择器
查看>>
06-jQuery的文档操作***
查看>>
red5 与tomcat集成配置 简单入门实例
查看>>
Vmware虚拟硬盘合并多个分割文件
查看>>
LeetCode-448. Find All Numbers Disappeared in an Array C#
查看>>
HDU 2364 (记忆化BFS搜索)
查看>>
紫书 例题 10-24 UVa 1641(面积计算)
查看>>
紫书 习题 10-21 UVa 1649 (组合数)
查看>>