使用PHP并行处理程序
【原文】
The proliferation of multicore CPUs and the inability of our learned CPU vendors to squeeze many more GHz into their designs means that often the only way to get additional performance is by writing clever parallel software.
【译文】
随着多核CPU的盛行以及我们所了解到的CPU生产商对CPU的运行功率提升的无能为力,这也就是意味着用户想要获得更优良的性能只能依靠于编写优秀的并行软件。
【原文】
One problem we were having is that some of our batch processing jobs were taking too long to run. In order to speed the processing, we tried to split the processing file into half, and let a separate PHP process run each job. Given that we were using a dual core server, each process would be able to run close to full speed (subject to I/O constraints).
【译文】
我们现在所面临的一个问题就是:运行批处理总是花费太多的时间。为了加快处理速度,我们尝试着把一个完整的处理文件一分为二,用单独的PHP进程来运行这两个文件。鉴于我们使用了双核服务器,每一个进程都会以接近全速的速率运转(受I/O限制)。
【原文】
Here is our technique for running multiple parallel jobs in PHP. In this example, we have two job files: j1.php and j2.php we want to run. The sample jobs don't do anything fancy. The file j1.php looks like this:
【译文】
以下是我们在PHP中运行并行任务的方法.在这个实例中,我们创建了两个文件:j1.php和j2.php,我们要运行它们。这个实例并不会做任何实际的工作。文件j1.php代码如下:
j1.php
$jobname = 'j1';
set_time_limit(0);
$secs = 60;
while ($secs) {
echo $jobname,'::',$secs,"\n";
flush(); @ob_flush(); ## make sure that all output is sent in real-time
$secs -= 1;
$t = time();
sleep(1); // pause
}
【原文】
The reason why we flush(); @ob_flush(); is that when we echo or print, the strings are sometimes buffered by PHP and not sent until later. These two functions ensure that all data is sent immediately.
【译文】
在程序中我们使用了flush();和@ob_flush();,原因是当我们使用echo和print输出时,字符串有的时候被PHP存入了缓存区,并且在稍后才会被发送出去。这两个函数用来保证所有的数据立刻被发送出去。
【原文】
We then have a 3rd file, control.php, which does the real work. This script will call j1.php and j2.php asynchronously using fsockopen in JobStartAsync(), so we are able to run j1.php and j2.php in parallel. The output from j1.php and j2.php are returned to control.php using JobPollAsync().
【译文】
接着我们创建第三个文件,control.php,它用来做实际的工作。该脚本通过JobStartAsync()函数中的fsockopen异步调用j1.php和j2.php,所以我们得以并行运行j1.php和j2.php。从j1.php和j2.php产生的输出将通过JobPollAsync()函数返回到control.php。
control.php
#
# control.php
#
function JobStartAsync($server, $url, $port=80,$conn_timeout=30, $rw_timeout=86400){
$errno = '';
$errstr = '';
set_time_limit(0);
$fp = fsockopen($server, $port, $errno, $errstr, $conn_timeout);
if (!$fp) {
echo "$errstr ($errno)
\n";
return false;
}
$out = "GET $url HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "Connection: Close\r\n\r\n";
stream_set_blocking($fp, false);
stream_set_timeout($fp, $rw_timeout);
fwrite($fp, $out); return $fp;
}
// returns false if HTTP disconnect (EOF), or a string (could be empty string) if still connected
function JobPollAsync(&$fp) {
if ($fp === false) return false;
if (feof($fp)) {
fclose($fp);
$fp = false;
return false;
}
return fread($fp, 10000);
}
##################################################
if (1) { /* SAMPLE USAGE BELOW */
$fp1 = JobStartAsync('localhost','/jobs/j1.php');
$fp2 = JobStartAsync('localhost','/jobs/j2.php');
while (true) {
sleep(1);
$r1 = JobPollAsync($fp1);
$r2 = JobPollAsync($fp2);
if ($r1 === false && $r2 === false) break;
echo "r1 = $r1
";
echo "r2 = $r2
";
flush(); @ob_flush();
}
echo "
Jobs Complete
";}
【原文】
And the output could look like this:
【译文】
输出类似于这样:
如下所示
r1 = HTTP/1.1 200 OK
Date: Wed, 03 Sep 2008 07:20:20 GMT
Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8d
X-Powered-By: Zend Core/2.5.0 PHP/5.2.5
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
7 j1::60
r2 = HTTP/1.1 200 OK
Date: Wed, 03 Sep 2008 07:20:20 GMT
Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8d
X-Powered-By: Zend Core/2.5.0 PHP/5.2.5
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
7 j2::60
----
r1 = 7 j1::59
r2 = 7 j2::59
----
r1 = 7 j1::58
r2 = 7 j2::58
----
【原文】
Note that "7 j2::60" is returned by PollJobAsync(). The reason is that the HTTP standard requires the packet to return the payload length (7 bytes) in the first line.
【译文】
注意一下PollJobAsync()返回的值:“7 j2::60”。产生该输出的原因是HTTP标准要求数据包在第一行返回有效载荷的长度(7字节)。
翻译难点及技巧:
the inability of our learned CPU vendors to squeeze many more GHz into their designs.
这句话应该算是本文中最难的一个句子了(但是不影响对全文的理解),简化一下句子就比较好翻译了,the inability of vendors to squeeze GHz into designs,生产商无力提高产品的运行功率。
翻译技巧
英语的一个重要特点就是喜欢省略,所以在翻译的过程中一定要把英语中省略掉得东西补全。
原文地址:http://phplens.com/phpeverywhere/?q=node/view/254
译者:杨洋
网名:我不叫大脸猫 PHPChina ID:qxhy123
性别:男 年龄:22岁
欧亚学院本科在校大学生,英语专业,资深PHP类技术资料翻译爱好者,狂热的PHP爱好者,精通搜索引擎优化技术(SEO)。致力于加速PHP技术本土化进程以及整合SEO+PHP。
个人博客地址:http://www.7bing.org






