为什么我的所有进程不是同时启动?

Why aren't all my processes starting at once?

我有一个将一堆数字相加的过程:

def slow(x):
    num = 0
    for i in xrange(int(1E9)):
        num += 1

我开始了其中的 500 个。

for x in range(500):
    out.write("Starting slow process - " + str(datetime.now()) + "\n")
    p = multiprocessing.Process(target = slow, args = (x, ))
    p.start()

我希望所有进程同时启动,因为我的计算机上允许的最大进程数大于 500。

user@computer$ cat /proc/sys/kernel/pid_max
32768

但是,一个进程的开始时间和下一个进程的开始时间之间有短暂的延迟。

Starting slow process - 2015-05-14 16:41:35.276839
Starting slow process - 2015-05-14 16:41:35.278016
Starting slow process - 2015-05-14 16:41:35.278666
Starting slow process - 2015-05-14 16:41:35.279328
Starting slow process - 2015-05-14 16:41:35.280053
Starting slow process - 2015-05-14 16:41:35.280751
Starting slow process - 2015-05-14 16:41:35.281444
Starting slow process - 2015-05-14 16:41:35.282094
Starting slow process - 2015-05-14 16:41:35.282720
Starting slow process - 2015-05-14 16:41:35.283364

随着我们启动更多进程,此延迟会变得更长:

Starting slow process - 2015-05-14 16:43:40.572051
Starting slow process - 2015-05-14 16:43:41.630004
Starting slow process - 2015-05-14 16:43:42.716438
Starting slow process - 2015-05-14 16:43:43.270189
Starting slow process - 2015-05-14 16:43:44.336397
Starting slow process - 2015-05-14 16:43:44.861934
Starting slow process - 2015-05-14 16:43:45.948424
Starting slow process - 2015-05-14 16:43:46.514324
Starting slow process - 2015-05-14 16:43:47.516960
Starting slow process - 2015-05-14 16:43:48.051986
Starting slow process - 2015-05-14 16:43:49.145923
Starting slow process - 2015-05-14 16:43:50.228910
Starting slow process - 2015-05-14 16:43:50.236215

造成这种现象的原因是什么?

您正在启动 500 个进程;每一个你都要求数到一百万来旋转。我不确定为什么您会惊讶于这需要时间?

启动 500 个进程即使什么都不做也会花费一些时间,但是当每个进程使用 python 数到一百万时,几乎可以肯定一两秒会过去。这些其他进程现在将竞争 CPU 时间,并且不能保证进行生成的进程会赢得这场比赛并立即生成其余进程。

编辑:您还对系统进行了 500 次调用以立即获取时间并打印它,如果您仅在开始和完成产卵时打印时间,这也需要一些时间,我怀疑这也会加快速度。

我怀疑如果您将计数循环替换为睡眠调用或类似性质的东西,这会更快,因此您所看到的根本不仅仅是启动进程的时间。

您的计算机并不喜欢 运行 比 CPU 内核更多的进程。通常,这没什么大不了的,因为没有进程占用 CPU。操作可以愉快的按照rules of its process scheduler.

依次给各个进程分配资源

当许多进程确实需要 CPU 时,坏事就会开始发生。操作系统会尽力而为,但速度可能会变慢。 None 的作业能够高效地完成任务。

随着您添加更多活动进程,情况会变得更糟。为什么会这样?

好吧,其中一个因素是 CPU 缓存在新进程接管时可能会有陈旧数据。 CPUs 有几级高速缓存,充当超高速内存。如果一个较长的 运行ning 进程获得对 CPU 的唯一访问权限,它将享受更快的速度,因为它将拥有全部缓存。

当进程数多于 CPUs 时,其中一些进程只是在队列中等待。当 OS 分配进程 CPU 时间时,将加载更多内存,等等,为下一个人减慢一切。

哦 - 我们不要忘记产卵过程也不是瞬时的。操作系统还有其他工作要做,例如确保您可以访问 Internet 和检查文件是否正在写入磁盘。

以下是根据@Agrajag 的建议对您的代码所做的一些更改,至少在我的系统上,这证实了他的怀疑。

  1. 在开始计算之前休眠 10 秒(以避免在内核尝试生成更多进程时占用 CPU)。
  2. 删除在生成过程中写入 out 的 IO 开销。

代码

import sys
import time
import multiprocessing
from datetime import datetime

def slow(x):
    time.sleep(10)
    num = 0
    for i in xrange(int(1E9)):
        num += 1

times = []
for x in range(500):
    times.append(datetime.now())
    p = multiprocessing.Process(target = slow, args = (x, ))
    p.start()

for x in times:
    sys.stdout.write("Starting slow process - " + str(x) + "\n")

尾部的示例输出。

Starting slow process - 2015-05-18 04:17:02.557117
Starting slow process - 2015-05-18 04:17:02.574186
Starting slow process - 2015-05-18 04:17:02.594736
Starting slow process - 2015-05-18 04:17:02.616716
Starting slow process - 2015-05-18 04:17:02.637369
Starting slow process - 2015-05-18 04:17:02.658615
Starting slow process - 2015-05-18 04:17:02.675418
Starting slow process - 2015-05-18 04:17:02.696439
Starting slow process - 2015-05-18 04:17:02.713795
Starting slow process - 2015-05-18 04:17:02.734777
Starting slow process - 2015-05-18 04:17:02.753063