【发布时间】:2019-02-12 18:20:24
【问题描述】:
我是多处理概念的新手。
我的代码
from multiprocessing import Process
def square(x):
for x in numbers:
print('%s squared is %s' % (x, x**2))
if __name__ == '__main__':
numbers = [43, 50, 5, 98, 34, 35]
p = Process(target=square, args=('x',))
p.start()
p.join
print "Done"
结果
Done
43 squared is 1849
50 squared is 2500
5 squared is 25
98 squared is 9604
34 squared is 1156
35 squared is 1225
我明白了,我们可以使用multiprocessing.cpu_count()来获取系统中的cpu数量
但是,我未能实现 2 件感兴趣的事情。 -
- 在所有 cpu 中平均分配所有任务
- 检查哪个 CPU 被哪个进程使用了
【问题讨论】:
标签: python parallel-processing multiprocessing python-multiprocessing