function runtime_init() { global $conf; $runtime = cache_get('runtime'); // 实时运行的数据,初始化! if ($runtime === NULL || !isset($runtime['users']) || !isset($runtime['onlines'])) { $runtime = array(); $runtime['users'] = user_count(); $runtime['posts'] = post_count(); $runtime['threads'] = thread_count(); $runtime['posts'] -= $runtime['threads']; // 减去首帖 $runtime['todayusers'] = 0; $runtime['todayposts'] = 0; $runtime['todaythreads'] = 0; $runtime['onlines'] = max(1, online_count()); // runtime_append $arr = kv_get('runtime_append'); is_array($arr) and $runtime += $arr; cache_set('runtime', $runtime, TRUE); } return $runtime; }
function runtask($url, $proxys) { $md5 = md5($url); $result = false; $retry = 1; //进程开始,添加进程符号 thread_count($md5); while ($result === false && $retry <= 10) { $proxy = $proxys[array_rand($proxys)]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_PROXY, $proxy); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_NOBODY, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $code = 0; if ($result != false) { $code = getCode($result); } if ($result != false && $code >= 200 && $code < 503) { //获取响应的头信息,统计业务已经足够了 $header = format_header($result); $header["url"] = $url; $header["time"] = time(); $header["date"] = date("Y-m-d\\TH:i:s\\Z", $header["time"]); list($header["ip"]) = explode(":", $proxy); //写入结果 $dir = DIR . "cache/result/" . date("Ymd") . "/" . substr($md5, 0, 2); if (!is_dir($dir)) { mkdir($dir, 0777, true); } $file = sprintf("%s/%s.json", $dir, $md5); file_put_contents($file, json_encode($header) . "\n", FILE_APPEND | LOCK_EX); } else { $retry++; $result = false; } } //进程结束,删除进程符号 thread_count($md5, true); return $result; }
//加载任务列表 include DIR . 'task.php'; $task = getTask(); if (empty($task)) { loger("No task found"); exit(1); } else { loger("Load task success, total " . count($task)); //清空进程锁目录 $dir = DIR . "cache/thread/*"; `rm -f {$dir}`; //如何实现异步多线程 foreach ($task as $job => $tasks) { foreach ($tasks as $url) { //进程上限锁 while (thread_count() > 10) { sleep(1); } $pid = pcntl_fork(); //父进程和子进程都会执行下面代码 if ($pid == -1) { //错误处理:创建子进程失败时返回-1. loger("Could not fork new thread !"); exit(1); } else { if ($pid) { //父进程会得到子进程号,所以这里是父进程执行的逻辑 pcntl_wait($status, WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。 } else { //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
$header['title'] = '后台管理'; $info = array(); $info['disable_functions'] = ini_get('disable_functions'); $info['allow_url_fopen'] = ini_get('allow_url_fopen') ? '是' : '否'; $info['safe_mode'] = ini_get('safe_mode') ? '是' : '否'; empty($info['disable_functions']) && ($info['disable_functions'] = '无'); $info['upload_max_filesize'] = ini_get('upload_max_filesize'); $info['post_max_size'] = ini_get('post_max_size'); $info['memory_limit'] = ini_get('memory_limit'); $info['max_execution_time'] = ini_get('max_execution_time'); $info['dbversion'] = $db->version(); $info['SERVER_SOFTWARE'] = array_value($_SERVER, 'SERVER_SOFTWARE', ''); $info['HTTP_X_FORWARDED_FOR'] = array_value($_SERVER, 'HTTP_X_FORWARDED_FOR', ''); $info['REMOTE_ADDR'] = array_value($_SERVER, 'REMOTE_ADDR', ''); $stat = array(); $stat['threads'] = thread_count(); $stat['posts'] = post_count(); $stat['users'] = user_count(); $stat['attachs'] = attach_count(); $stat['disk_free_space'] = function_exists('disk_free_space') ? humansize(disk_free_space('./')) : '未知'; $lastversion = get_last_version($stat); // 潜在错误检测,目录可写检测,避免搬家导致的问题。 $check = array(); $upload_tmp_dir = ini_get('upload_tmp_dir'); if (!empty($upload_tmp_dir)) { $check['upload_path_check'] = is_writable($upload_tmp_dir) ? "<span class\"red\">{$upload_tmp_dir} 不可写</span>,上传功能会受到影响。" : "<span class=\"green\">{$upload_tmp_dir} 可写</span>"; } else { $check['upload_path_check'] = "<span class=\"red\">php.ini 中未设置 upload_tmp_dir,可能会导致上传失败 </span>"; } $check['php_ini'] = ini_get('upload_tmp_dir'); include './admin/view/index.htm';