3

对于我的网站,我每 30 分钟运行一次 Cron 作业,如果我尝试在这 30 分钟标记访问我的网站,我会收到 500 内部服务器错误,或者我会收到网站上任何页面的 5-10 秒加载时间。

我正在使用 Php,而我的 cron 作业正在使用 Php 和 MySQL。

我怎样才能使它不落后于我的整个网站或使它更快,从而减少滞后?

克朗斯:在此处输入图像描述

每 15 分钟运行一次的 crons 之一:

<?php
require('functions.php');

global $mysqli;

$select = $mysqli->prepare("SELECT `tkn` FROM `users` ORDER BY `dt` ASC LIMIT 0, 200");
$select->execute();
$select->bind_result($cur_token);

$tokens = array();

while($select->fetch())
{
    array_push($tokens, $cur_token);
}

foreach($tokens as $token)
{
    $api = Class::Instance($token);
    $info = $api->Users->Info();

    if(empty($info->error))
    {
        $info->data->token = $token;
        updateUser($info->data);

    } else if($info->error->code == 400) {
        $update = $mysqli->prepare("UPDATE `users` SET `active` = 0 WHERE `tkn` = ?");
        $update->bind_param('s', $token);
        $update->execute();
    }
}

?>

其他 Cron 作业:

<?php
require('functions.php');

global $mysqli;

$select = $mysqli->prepare("SELECT `email`, `username`, `id` FROM `users` WHERE `email` IS NOT NULL AND `email` <> '' AND `credits` < `credits_offered` AND `emailed_credits` = 0");
$select->execute();
$select->bind_result($email, $username, $id);

$users = array();

while($select->fetch())
{
    $users[] = array("id" => $id, "email" => $email, "username" => $username);
}

foreach($users as $user)
{
    $to      = $user['email'];
    $subject = '';
    $message = "";
    $headers = 'From: email@domain.com' . "\r\n" .
        'Reply-To: reply@domain.com';

    mail($to, $subject, $message, $headers);

    $update = $mysqli->prepare("UPDATE `users` SET `emailed` = 1 WHERE `id` = ?");
    $update->bind_param('i', &$user['id']);

    $update->execute();
    $update->close();
}


?>

我怎样才能减少这些滞后或根本不滞后?

4

2 回答 2

2

第一个建议是将准备调用移出循环。更新将锁定您的表一段时间,因此只有在完成操作后才更新......将需要更新的内容缓存在数组中并启动事务,执行所有更新而不是提交。更新了邮件脚本的代码示例

<?php
require('functions.php');

global $mysqli;

$select = $mysqli->prepare("SELECT `email`, `username`, `id` FROM `users` WHERE `email` IS NOT NULL AND `email` <> '' AND `credits` < `credits_offered` AND `emailed_credits` = 0");
$select->execute();
$select->bind_result($email, $username, $id);

$users = array();

while($select->fetch())
{
    $users[] = array("id" => $id, "email" => $email, "username" => $username);
}

$u = array();

foreach($users as $user)
{
    $to      = $user['email'];
    $subject = '';
    $message = "";
    $headers = 'From: email@domain.com' . "\r\n" .
        'Reply-To: reply@domain.com';

    mail($to, $subject, $message, $headers);
    $u[] = $user['id'];

}
$update = $mysqli->prepare("UPDATE `users` SET `emailed` = 1 WHERE `id` = ?");
$update->bind_param('i', &$i);
$mysqli->autocommit(false);
foreach($u as $i)
    $update->execute();
$update->close();
$mysqli->commit();

哦,差点忘了,大多数mysql存储引擎都很笨,确保你使用InnoDB,那个有事务支持。或者,如果您使用“愚蠢”的存储引擎,如果您的 users.id 是 int ,您仍然可以欺骗它进行单个事务以更新多行

$update = $mysqli->prepare('UPDATE users SET emailed=1 WHERE id IN (' . implode(',', $u) . ')');
$update->execute();
$update->close();

而是在关闭foreach($users as $user)语句之后

于 2012-09-17T23:21:08.793 回答
-5

编写更高效的脚本或获得更好的硬件。

编辑:

就群发电子邮件而言,您可以在脚本中添加超时,这样它就不会尽快处理所有电子邮件。这将减少 CPU 的负载,但会使您的脚本花费更长的时间。

例如:

for(all emails)
{
   //send email
   sleep(5);
}
于 2012-09-17T16:40:11.697 回答