php pcntl效率,php-使用pcntl_fork()提高HTML抓取工具的效率

在前两个问题的帮助下,我现在有了一个运行中的HTML抓取工具,可将产品信息输入数据库.我现在想做的是通过使我的刮板与pcntl_fork配合使用来有效地改善大脑.

如果我将php5-cli脚本分成10个单独的块,则会在很大程度上提高总运行时间,因此我知道我不受I / O或CPU的限制,而仅受我的抓取函数的线性性质的限制.

使用从多个来源收集来的代码,我进行了以下工作测试:

libxml_use_internal_errors(true);

ini_set('max_execution_time', 0);

ini_set('max_input_time', 0);

set_time_limit(0);

$hrefArray = array("http://slashdot.org", "http://slashdot.org", "http://slashdot.org", "http://slashdot.org");

function doDomStuff($singleHref,$childPid) {

$html = new DOMDocument();

$html->loadHtmlFile($singleHref);

$xPath = new DOMXPath($html);

$domQuery = '//div[@id="slogan"]/h2';

$domReturn = $xPath->query($domQuery);

foreach($domReturn as $return) {

$slogan = $return->nodeValue;

echo "Child PID #" . $childPid . " says: " . $slogan . "\n";

}

}

$pids = array();

foreach ($hrefArray as $singleHref) {

$pid = pcntl_fork();

if ($pid == -1) {

die("Couldn't fork, error!");

} elseif ($pid > 0) {

// We are the parent

$pids[] = $pid;

} else {

// We are the child

$childPid = posix_getpid();

doDomStuff($singleHref,$childPid);

exit(0);

}

}

foreach ($pids as $pid) {

pcntl_waitpid($pid, $status);

}

// Clear the libxml buffer so it doesn't fill up

libxml_clear_errors();

这就提出了以下问题:

1)给定我的hrefArray包含4个网址-如果该数组包含1000个产品网址,那么此代码将产生1000个子进程?如果是这样,最好的方法是将处理数量限制为10个,再以1,000个网址为例,将子工作量划分为每个孩子100个产品(10 x 100).

2)我了解到pcntl_fork创建了该流程以及所有变量,类等的副本.我想做的是用DOMDocument查询替换我的hrefArray变量,该查询建立了要抓取的产品列表,然后将其输入由子进程来执行处理-因此将负载分散到10个子进程中.

我的大脑告诉我需要执行以下操作(显然这不起作用,因此请不要运行它):

libxml_use_internal_errors(true);

ini_set('max_execution_time', 0);

ini_set('max_input_time', 0);

set_time_limit(0);

$maxChildWorkers = 10;

$html = new DOMDocument();

$html->loadHtmlFile('http://xxxx');

$xPath = new DOMXPath($html);

$domQuery = '//div[@id=productDetail]/a';

$domReturn = $xPath->query($domQuery);

$hrefsArray[] = $domReturn->getAttribute('href');

function doDomStuff($singleHref) {

// Do stuff here with each product

}

// To figure out: Split href array into $maxChilderWorks # of workArray1, workArray2 ... workArray10.

$pids = array();

foreach ($workArray(1,2,3 ... 10) as $singleHref) {

$pid = pcntl_fork();

if ($pid == -1) {

die("Couldn't fork, error!");

} elseif ($pid > 0) {

// We are the parent

$pids[] = $pid;

} else {

// We are the child

$childPid = posix_getpid();

doDomStuff($singleHref);

exit(0);

}

}

foreach ($pids as $pid) {

pcntl_waitpid($pid, $status);

}

// Clear the libxml buffer so it doesn't fill up

libxml_clear_errors();

但是我不知道如何仅在主/父进程中构建hrefsArray []并将其反馈给子进程.目前,我尝试过的所有操作都会在子进程中引起循环.即my hrefsArray内置在主服务器以及后续的每个子进程中.

我敢肯定,我将彻底解决所有这些错误,因此,只需要向正确的方向轻轻推一下就可以了.

解决方法:

似乎我每天都建议这样做,但是您是否看过Gearman?甚至还有一个有据可查的PECL class.

Gearman是一个工作队列系统.您将创建连接和侦听作业的工作程序,以及连接和发送作业的客户端.客户端可以等待所请求的作业完成,也可以将其触发而忘记.根据您的选择,工作人员甚至可以发送状态更新以及他们在此过程中进行的工作.

换句话说,您可以获得多个进程或线程的好处,而不必担心进程和线程.客户和工人甚至可以在不同的机器上.

标签:pcntl,php,fork

来源: https://codeday.me/bug/20191012/1898070.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值