这个问题被弹出了好几次,但我找不到合适的答案。我想现在我找到了一个很好的解决方案!
不幸parallel
的是,它不是标准发行版的一部分,但make是。它有一个开关-j
要做,使并行。
man make(1) ]: (有关 make 并行执行的更多信息)
-j [jobs], --jobs[=jobs]
Specifies the number of jobs (commands) to run simultaneously. If
there is more than one -j option, the last one is effective. If
the -j option is given without an argument, make will not limit
the number of jobs that can run simultaneously.
所以只要有适当Makefile
的问题就可以解决。
.PHONY: all $(PHP_DEST)
# Create an array of targets in the form of PHP1 PHP2 ... PHP90
PHP_DEST := $(addprefix PHP, $(shell seq 1 1 90))
# Default target
all: $(PHP_DEST)
# Run the proper script for each target
$(PHP_DEST):
N=$(subst PHP,,$@); php path$N/some_job_$N.php
PHP#
它每次调用都会创建 90 个目标php path#/some_job_#.php
。如果您运行make -j 5
,那么它将运行 5 个 php 并行实例。如果一个完成,它将开始下一个。
我将其重命名Makefile
为parallel.mak
,然后运行chmod 700 parallel.mak
并添加#!/usr/bin/make -f
到第一行。现在它可以称为./parallel.mak -j 5
.
甚至您可以使用更复杂的-l
开关:
-l [load], --load-average[=load]
Specifies that no new jobs (commands) should be started if there
are others jobs running and the load average is at least load (a
floating-point number). With no argument, removes a previous load
limit.
在这种情况下,make将根据系统的负载决定可以启动多少个作业。
我对其进行了测试./parallel.mak -j -l 1.0
并且运行良好。它起初并行启动了 4 个程序,相反-j
,没有 args 意味着尽可能多地并行运行进程。