Flask接口签名sign原理与实例代码浅析
354
2022-08-25
python并发编程实战(九):使用多进程multiprocessing模块加速程序的运行(python多进程实现并发)
有了多线程threading,为什么还要用多进程multiprocessing
多进程multiprocessing知识梳理(对比多线程threading)
代码实战:单线程、多线程、多进程对比CPU密集计算速度
tmp/06.thread_process_cpu_bound.py
import mathfrom concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutorimport timePRIMES = [112272535095293] * 100def is_prime(n): if n < 2: return False if n == 2: return True if n % 2 == 0: return False sqrt_n = int(math.floor(math.sqrt(n))) for i in range(3, sqrt_n + 1, 2): if n % i == 0: return False return Truedef single_thread(): for number in PRIMES: is_prime(number)def multi_thread(): with ThreadPoolExecutor() as pool: pool.map(is_prime, PRIMES)def multi_process(): with ProcessPoolExecutor() as pool: pool.map(is_prime, PRIMES)if __name__ == '__main__': start = time.time() single_thread() end = time.time() print("single_thread, cost: ", end - start, "seconds") start = time.time() multi_thread() end = time.time() print("multi_thread, cost: ", end - start, "seconds") start = time.time() multi_process() end = time.time() print("multi_process, cost: ", end - start, "seconds")
运行结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~