forked from xwmhmily/YOF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcntl.php
executable file
·56 lines (47 loc) · 1.16 KB
/
pcntl.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* 多进程采集
* Author: 大眼猫
* Remark: 请在 CLI 模式下运行!
*/
$final = array();
define('LIB_PATH', __DIR__.'/application/library');
include LIB_PATH.'/phpQuery/phpQuery.php';
function run($page){
$destination = "http://www.ttpet.com/zixun/42/category-catid-42-$page.html";
echo 'Crawling '.$destination."\n";
phpQuery::newDocumentFile($destination);
$articles = pq('#main_bg .zixunmain .p_lf .p_pad')->find('ul');
foreach($articles as $article) {
$m['title'] = pq($article)->find('dl dd a')->html();
$final[] = $m;
}
echo '=========== Page ===========> '.$page."\r\n";
print_r($final);
}
$page = 1;
$pid_arr = array();
// 开5个进程
while ($page <= 5){
$pid = pcntl_fork();
if ($pid){
// 这里是父进程的代码
$pid_arr[] = $pid;
}else{
// 这里是子进程的代码
echo "子进程ID => ".posix_getpid()."\r\n";
call_user_func('run', $page);
exit(0);
}
$page++;
}
// 等待子进程退出
while(count($pid_arr) > 0) {
$myID = pcntl_waitpid(-1, $status, WNOHANG);
foreach($pid_arr as $key => $pid) {
if($myID == $pid){
unset($pid_arr[$key]);
}
}
usleep(100);
}