python 在不同CPU上同时运行多个程序
Posted Marks & Co
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在不同CPU上同时运行多个程序相关的知识,希望对你有一定的参考价值。
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上同时运行多个程序的主要内容,如果未能解决你的问题,请参考以下文章