修改代码 并行与并发
可以帮忙修改下代码吗?使代码可以成功跑起import time
from threading import Thread
from multiprocessing import Process
def is_prime(n):
""" 判断n是否为素数 """
if n == 1:
return False
else:
assert isinstance(n, int)
num = int(n ** 0.5)
for i in range(2, num + 1):
if n % i == 0:
return False
return True
def sum_primes(n):
""" 计算小于等于n的素数之和 """
sump = 0
i = 0
for i in range(0, n + 1):
if is_prime(i) == True:
sump = sump + i
i = i + 1
print(sump, end=" ")
return sump
def io(n):
""" 模拟IO密集型 """
time.sleep(1)
print(n, end=" ")
return n
def debug(func):
""" 装饰器,记录运行时间
提示:在函数执行前执行start_time = time.time()获取开始时间
在函数执行后执行end_time = time.time()获取完成时间
"""
def int_time(*args, **kwargs):
start_time = time.time()# 程序开始时间
func()
end_time = time.time()# 程序结束时间
total_time = end_time - start_time
print('程序共计%s秒' % total_time)
return int_time
@debug
def for_work(func, numbers):
""" 顺序执行工作 """
for n in numbers:
func(n)
@debug
def thread_work(func, numbers):
""" 并发 """
th = []
for n in numbers:
t = Thread(target=func)
th.append(t)
for n in numbers:
t.start()
for n in numbers:
t.join()
@debug
def process_work(func, numbers):
""" 并行 """
th = []
for n in numbers:
t = Process(target=func)
th.append(t)
for n in numbers:
t.start()
for n in numbers:
t.join()
def main():
numbers = (100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000)
# CPU密集型
for_work(sum_primes, numbers)
thread_work(sum_primes, numbers)
process_work(sum_primes, numbers)
# IO密集型
for_work(io, numbers)
thread_work(io, numbers)
process_work(io, numbers)
if __name__ == "__main__":
main()
本帖最后由 Twilight6 于 2020-6-10 16:11 编辑
第一个报错是因为你参数少了两,我也不懂传入的对不对,对修饰符和线程了解很少,所以代码几乎看不懂:
func()# <--这边要两个参数才能运行
瞎改成:
func(*args,**kwargs)
完整代码:
import time
from threading import Thread
from multiprocessing import Process
def is_prime(n):
""" 判断n是否为素数 """
if n == 1:
return False
else:
assert isinstance(n, int)
num = int(n ** 0.5)
for i in range(2, num + 1):
if n % i == 0:
return False
return True
def sum_primes(n):
""" 计算小于等于n的素数之和 """
sump = 0
for i in range(n + 1):
if is_prime(i) == True:
sump = sump + i
print(sump, end=" ")
return sump
def io(n):
""" 模拟IO密集型 """
time.sleep(1)
print(n, end=" ")
return n
def debug(func):
""" 装饰器,记录运行时间
提示:在函数执行前执行start_time = time.time()获取开始时间
在函数执行后执行end_time = time.time()获取完成时间
"""
def int_time(*args, **kwargs):
start_time = time.time()# 程序开始时间
func(*args,**kwargs)
end_time = time.time()# 程序结束时间
total_time = end_time - start_time
print('%s程序共计%.3f秒' % (func.__name__,total_time))
return int_time
@debug
def for_work(func, numbers):
""" 顺序执行工作 """
for n in numbers:
func(n)
@debug
def thread_work(func, numbers):
""" 并发 """
th = []
for n in numbers:
t = Thread(target=func,args=(n,))
th.append(t)
t.start()
t.join()
@debug
def process_work(func, numbers):
""" 并行 """
th = []
for n in numbers:
t = Process(target=func,args=(n,))
th.append(t)
t.start()
t.join()
def main():
numbers = (100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000)
# CPU密集型
for_work(sum_primes, numbers)
thread_work(sum_primes, numbers)
process_work(sum_primes, numbers)
# IO密集型
for_work(io, numbers)
thread_work(io, numbers)
process_work(io, numbers)
if __name__ == "__main__":
main()
页:
[1]