首页 > 编程语言 > 详细

python 在不同CPU上同时运行多个程序

时间:2018-10-19 21:09:35      阅读:241      评论:0      收藏:0      [点我收藏+]

出处/From https://www.quora.com/If-you-run-Python-under-a-dual-core-CPU-then-can-you-run-two-Python-programs-at-once-one-that-utilizes-1-core-and-the-other-utilizing-the-other-core

In [24]: import os
In [25]: import numpy as np
In [26]: from multiprocessing import Process
In [27]: class MyProc(Process):
    ...:     def __init__(self, num):
    ...:         self.num = num
    ...:         super().__init__()
    ...:
    ...:     def run(self):
    ...:         print(f‘Process {self.num} starting...PID {os.getpid()}‘)
    ...:         np.random.seed(self.num)
    ...:         total = np.sum([np.random.randint(10_000)
                        for _ in range(10_000_000)])
    ...:         print(f‘Total sum = {total}‘)
    ...:
 
In [28]: procs = [MyProc(i) for i in range(2)]
 
In [29]: for p in procs:
    ...:     p.start()
    ...:
 
Process 0 starting...PID 92402
Process 1 starting...PID 92403
In [30]: for p in procs:
    ...:     p.join()
    ...:
Total sum = 50003085304
Total sum = 49988200971

python 在不同CPU上同时运行多个程序

原文:https://www.cnblogs.com/yaos/p/9818625.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!