【发布时间】:2012-10-19 20:01:38
【问题描述】:
大家好!
我们需要一些帮助来开发和实现多卷曲功能到我们的爬虫中。我们有大量“要扫描的链接”,我们用 Foreach 循环抛出它们。
让我们用一些伪代码来理解逻辑:
1) While ($links_to_be_scanned > 0).
2) Foreach ($links_to_be_scanned as $link_to_be_scanned).
3) Scan_the_link() and run some other functions.
4) Extract the new links from the xdom.
5) Push the new links into $links_to_be_scanned.
5) Push the current link into $links_already_scanned.
6) Remove the current link from $links_to_be_scanned.
现在,我们需要定义最大并行连接数,并能够为每个链接并行运行此过程。
我知道我们将不得不创建一个 $links_being_scanned 或某种队列。
说实话,我真的不知道如何解决这个问题,如果有人能提供一些解决方案或想法,将不胜感激。
提前致谢! 克里斯;
扩展:
我刚刚意识到,复杂的不是多卷曲本身,而是请求后每个链接完成的操作量。
即使在 muticurl 之后,我最终还是必须找到一种方法来并行运行所有这些操作。下面描述的整个算法必须并行运行。
所以现在重新考虑,我们将不得不做这样的事情:
While (There's links to be scanned)
Foreach ($Link_to_scann as $link)
If (There's less than 10 scanners running)
Launch_a_new_scanner($link)
Remove the link from $links_to_be_scanned array
Push the link into $links_on_queue array
Endif;
每个扫描仪都会这样做(这应该并行运行):
Create an object with the given link
Send a curl request to the given link
Create a dom and an Xdom with the response body
Perform other operations over the response body
Remove the link from the $links_on_queue array
Push the link into the $links_already_scanned array
我假设我们可以使用扫描仪算法创建一个新的 PHP 文件,并为每个并行进程使用 pcntl_fork()?
由于即使使用多卷曲,我最终还是不得不等待其他进程在常规 foreach 结构上循环。
我想我必须使用 fsockopen 或 pcntl_fork 来解决这个问题。
我们将不胜感激建议、cmets、部分解决方案,甚至是“祝你好运”!
非常感谢!
【问题讨论】:
标签: php curl parallel-processing fork fsockopen