计算型密集,多进程执行

"""
计算型密集,多进程执行
# 自定义个计算型密集任务,去完成测试普通执行,和多进程执行的区别
get_nowait(),可以判断队列是否为空
math模块和python内置的模块,都存在有pow()函数,pow(x,y)等价与x**y   例如pow(2,3)  内置输出8,math模块输出8.0
multiprocessing.Queue()和queue.Queue() 不一样,当使用多进程时,使用multiprocessing

countlist=[1,5,8,9]
count =2
count += 1 if count in countlist else 0
写文件时,循环放在打开文件里面,可以减少文件的打开次数
"""
import threading
import time
import multiprocessing
import logging

logging.basicConfig(filename="",level=logging.INFO)
new_queue = multiprocessing.Queue()


def init_queue():
    for abum in range(10):
        new_queue.put(abum)
    return new_queue


class 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-startTime


class 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值