PHP[006]:单进程多任务并行处理实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
$i = 1;

$str = <<<EOF
<?php
\$logName = basename(__FILE__);
\$i =1;

\$result = '';
\$result .= "初始: " . memory_get_usage() . "B\n";
\$result .= "使用: " . memory_get_usage() . "B\n";
while(\$i--){
sleep(10);

\$result .= "释放: " . memory_get_usage() . "B\n";
\$result .= "峰值: " . memory_get_peak_usage() . "B\n";
file_put_contents(__DIR__.'/'.\$logName.'.log',time().PHP_EOL.\$result,FILE_APPEND);
}
EOF;
$file_name = [];
while ($i < 10) {
$file_name[] = 'timeEvent' . $i . '.php';
file_put_contents(__DIR__ . '/' . 'timeEvent' . $i . '.php', $str);
$i++;
}

var_dump($file_name);

foreach ($file_name as $name) {

$strRun = 'php ' . __DIR__ . '/' . $name . ' &';
$waitClose = pclose(popen($strRun, 'r'));
echo time() . PHP_EOL;
}
$result = '';
$result .= "初始: " . memory_get_usage() . "B\n";
$result .= "使用: " . memory_get_usage() . "B\n";
$result .= "释放: " . memory_get_usage() . "B\n";
$result .= "峰值: " . memory_get_peak_usage() . "B\n";