"""
计算型密集,多进程执行
# 自定义个计算型密集任务,去完成测试普通执行,和多进程执行的区别
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