pcntl_fork() 函数

发布于 2024-09-19 08:08:40 字数 3113 浏览 10 评论 0原文

因为爬行网络可能会花费大量时间,所以我想让 pcntl_fork() 帮助我创建多个子项以将我的代码分成几部分。

  1. 主 - 爬行域
  2. 子 - 接收链接时,子必须爬行在域中找到的链接
  3. 子 - 接收新链接时,必须执行与 2. 相同的操作。

我可以随心所欲地制作多少个孩子,还是必须设定最多的孩子数量?

这是我的代码:

class MyCrawler extends PHPCrawler 
{


  function handlePageData(&$page_data) 
  { // CHECK DOMEIN
$domain = $_POST['domain'];
$keywords = $_POST['keywords'];
//$tags = get_meta_tags($page_data["url"]);
//$iKeyFound = null;


$find = $keywords;
$str = file_get_contents($page_data["url"]);
if(strpos($str, $find) == true && $page_data["received"] == true)
{           
    $keywords = $_POST['keywords'];
    if($page_data["header"]){
    echo "<table border='1' >";
    echo "<tr><td width='300'>Status:</td><td width='500'> ".strtok($page_data["header"], "\n")."</td></tr>";}
    else "<table border='1' >";

    // PRINT EERSTE LIJN

    echo "<tr><td>Page requested:</td><td> ".$page_data["url"]."</td></tr>";
    // PRINT STATUS WEBSITE

    // PRINT WEBPAGINA
    echo "<tr><td>Referer-page:</td><td> ".$page_data["referer_url"]."</td></tr>";

    // CONTENT ONTVANGEN?
    if ($page_data["received"]==true)
      echo "<tr><td>Content received: </td><td>".$page_data["bytes_received"] / 8 . " Kbytes</td></tr></table>";
    else
      echo "<tr><td>Content:</td><td> Not received</td></tr></table>";


    $domain = $_POST['domain'];
    $link = mysql_connect('localhost', 'crawler', 'DRZOIDBERGGG');

    if (!$link) 
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("crawler");
    if(empty($page_data["referer_url"]))
    $page_data["referer_url"] = $page_data["url"];

    strip_tags($str, '<p><b>');
    $matches = $keywords;
    //$match = preg_match_all("'/<(*.?)(*.?)>(*.?)'".$keywords."'(*.?)<\/($1)>/'", $str, $matches, PREG_SET_ORDER);
    //echo $match;

    $doc = new DOMDocument();
    $doc->loadHTML($str);

    $xPath = new DOMXpath($doc);
    $xPathQuery = "//text()[contains(translate(.,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '".strtoupper($keywords)."')]";
    $elements = $xPath->query($xPathQuery);

    if($elements->length > 0){

    foreach($elements as $element){
        print "Gevonden: " .$element->nodeValue."<br />";
    }}

    $result = mysql_query("SELECT * FROM crawler WHERE data = '".$element->nodeValue."' ") ;

    if(mysql_num_rows($result)>0)
    echo 'Column already exist';

    else{ 
    echo 'added';
    mysql_query("INSERT INTO crawler (id, domain, url, keywords, data) VALUES ('', '".$page_data["referer_url"]."', '".$page_data["url"]."', '".$keywords."', '".$element->nodeValue. "' )");
    }

    echo '<br>';
    echo "<br><br>";
    echo str_pad(" ", 5000); // "Force flush", workaround
    flush();



}

忘记说:我需要一个 WIN x(86) 32 位解决方法!

因为我的客户端不支持它。

Because crawling the web can cost a lot of time I want to let pcntl_fork() help me in creating multiple childs to split my code in parts.

  1. Master - crawling the domain
  2. Child - When receiving a link child must crawl the link found on the domain
  3. Child - Must do the same as 2. when receiving new link.

Can i make as many as i want, or do i have to set a maximum of childs?

Here's my code:

class MyCrawler extends PHPCrawler 
{


  function handlePageData(&$page_data) 
  { // CHECK DOMEIN
$domain = $_POST['domain'];
$keywords = $_POST['keywords'];
//$tags = get_meta_tags($page_data["url"]);
//$iKeyFound = null;


$find = $keywords;
$str = file_get_contents($page_data["url"]);
if(strpos($str, $find) == true && $page_data["received"] == true)
{           
    $keywords = $_POST['keywords'];
    if($page_data["header"]){
    echo "<table border='1' >";
    echo "<tr><td width='300'>Status:</td><td width='500'> ".strtok($page_data["header"], "\n")."</td></tr>";}
    else "<table border='1' >";

    // PRINT EERSTE LIJN

    echo "<tr><td>Page requested:</td><td> ".$page_data["url"]."</td></tr>";
    // PRINT STATUS WEBSITE

    // PRINT WEBPAGINA
    echo "<tr><td>Referer-page:</td><td> ".$page_data["referer_url"]."</td></tr>";

    // CONTENT ONTVANGEN?
    if ($page_data["received"]==true)
      echo "<tr><td>Content received: </td><td>".$page_data["bytes_received"] / 8 . " Kbytes</td></tr></table>";
    else
      echo "<tr><td>Content:</td><td> Not received</td></tr></table>";


    $domain = $_POST['domain'];
    $link = mysql_connect('localhost', 'crawler', 'DRZOIDBERGGG');

    if (!$link) 
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("crawler");
    if(empty($page_data["referer_url"]))
    $page_data["referer_url"] = $page_data["url"];

    strip_tags($str, '<p><b>');
    $matches = $keywords;
    //$match = preg_match_all("'/<(*.?)(*.?)>(*.?)'".$keywords."'(*.?)<\/($1)>/'", $str, $matches, PREG_SET_ORDER);
    //echo $match;

    $doc = new DOMDocument();
    $doc->loadHTML($str);

    $xPath = new DOMXpath($doc);
    $xPathQuery = "//text()[contains(translate(.,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '".strtoupper($keywords)."')]";
    $elements = $xPath->query($xPathQuery);

    if($elements->length > 0){

    foreach($elements as $element){
        print "Gevonden: " .$element->nodeValue."<br />";
    }}

    $result = mysql_query("SELECT * FROM crawler WHERE data = '".$element->nodeValue."' ") ;

    if(mysql_num_rows($result)>0)
    echo 'Column already exist';

    else{ 
    echo 'added';
    mysql_query("INSERT INTO crawler (id, domain, url, keywords, data) VALUES ('', '".$page_data["referer_url"]."', '".$page_data["url"]."', '".$keywords."', '".$element->nodeValue. "' )");
    }

    echo '<br>';
    echo "<br><br>";
    echo str_pad(" ", 5000); // "Force flush", workaround
    flush();



}

FORGOT TO SAY: I NEED A WIN x(86) 32 bits workaround!

Because it's not supported on my client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-09-26 08:08:40

我想知道使用 Gearman 之类的东西是否会更好。

它是一个在您的系统上运行的作业管理器,您向它提交作业(如果您愿意,可以通过 php),然后它将它们分配给工作人员(同样,用 php 编写),然后工作人员报告结果。它非常强大且灵活,您可以让它运行更多工作人员来处理更多工作负载。

I wonder if you wouldn't be better served by going with something like Gearman for this.

It's a job manager that runs on your system and you submit jobs to it (via php if you like), and then it assigns them to workers (again, written in php), who then report back with their result. It's pretty robust and flexible in that you can let it run more workers to handle more workload.

要走就滚别墨迹 2024-09-26 08:08:40

shell_exec 做了这个事情但不知道如何使用。

shell_exec does the thing but don't know how to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文