尝试添加大量行时脚本超时
我正在尝试使用 cakephp (用于 QR 码链接系统)将大量记录添加到我的数据库中。
我的基本代码是这样的:
public function generateQrCodes($program_id = null, $amount = 10000)
{
//add entries into database & generate QR images
for ($i = 1; $i++; $i <= $amount) {
$this->_createQr($program_id, $i);
}
//generate ZIP file
}
private function _createQr($program_id, $number)
{
//save into database
$this->create();
$this->data['Qrcode']['program_id'] = $program_id;
$this->data['Qrcode']['qr'] = $number;
$saved = $this->save();
if ($saved) {
$this->_createQrImage($program_id, $number);
return true;
} else {
return false;
}
}
显然这会超时(大约 900 行之后)。有人可以帮我想出一种方法来重写它,这样它就不会超时(以某种方式将其分成块)吗?
I'm trying to add a large amount of records into my database using cakephp
(for a QR Code linking system).
My base code goes like this:
public function generateQrCodes($program_id = null, $amount = 10000)
{
//add entries into database & generate QR images
for ($i = 1; $i++; $i <= $amount) {
$this->_createQr($program_id, $i);
}
//generate ZIP file
}
private function _createQr($program_id, $number)
{
//save into database
$this->create();
$this->data['Qrcode']['program_id'] = $program_id;
$this->data['Qrcode']['qr'] = $number;
$saved = $this->save();
if ($saved) {
$this->_createQrImage($program_id, $number);
return true;
} else {
return false;
}
}
Obviously this times out (after about 900ish rows). Can someone help me work out a way to rewrite this so that it wont timeout (break it into chunks somehow)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要多长时间:$this->_createQrImage($program_id, $number); ?
您可以在 shell 中执行此操作,这样它就不会超时。
http://book.cakephp.org/1.3/en/view/1107/Creating-Shells-Tasks
How long it takes to : $this->_createQrImage($program_id, $number); ?
You could probably do this in a shell so it will not timeout..
http://book.cakephp.org/1.3/en/view/1107/Creating-Shells-Tasks
或者,您可以将 php.ini 中的 max_execution_time 指令修改为更大的值。默认时间为 30 秒。如果您无权访问主 php.ini(例如您位于共享主机上),那么您可以在自己的目录中创建 php.ini 的本地副本,它将允许您覆盖需要更改的那些指令。
http://www.php.net/manual /en/info.configuration.php#ini.max-execution-time
Alternatively you can modify the max_execution_time directive in php.ini to a larger value. It's default time is 30 seconds. If you don't have access to the main php.ini such as you are on shared hosting then you can create a local copy of php.ini in your own directories and it will allow you to override those directives that you need to change.
http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time