PHP 批处理 处理大数据 长时间处理
最近在做一个东西,用PHP写的,由于要将数据库中的图片名字读出来,然后访问对应图片,读取图片的大小以及分辨率,重新存入数据库。数据量现在才是处理5000条的,如果不加任何设置的话,php默认最大执行时间是30秒。
所以30秒内,我只能处理700条,刚开始只能手动,七百、七百的执行了。后来感觉这样不行,以后的数据量会过10W,这样会累死我的。
网上搜索了下,有三种处理方式。
第一种
很直接,既然你默认30秒,那么我就把你的默认时间设置长点,或者直接没有限制。
在php中添加,php ini_set('max_execution_time', '100');函数就可以了,100代表100秒,如果你设置了0,那么就代表没有限制。(ps:我用的是zend的结合,设置为0的时候,不是没有限制,而是大概是120的样子,我的解决方法是,把这个值设置的so big)
第二种
本人很喜欢,是利用php和批处理的结合。
[php]
@REM
@REM =='
@SET PHPCLI="D:\Program Files\Zend\Zend Studio - 8.0.1\plugins\org.zend.php.debug.debugger.win32.x86_5.3.18.v20110322\resources\php53\php.exe"
@REM PHPCLI 设置你的php.exe的路径
@%PHPCLI% %0
@goto :EOF
@REM';?>
include'C:\\Program Files\\Zend\\Apache2\\htdocs\\pic_gather\\pic_operate.php';
echo "\rfinish";
ob_flush();
exec("pause");
?>
是批处理命令,所以我还花了1个小时特地看了下批处理命令。大致翻译下上面的程序。
@:不显示后面的指令,如果去掉,指令会在命令窗口中打印出来
REM 是批处理的注释,后面的是注释,不执行。跟@不同,@只是命令不打印出来,并不表示注释。
程序开始执行,首先是bat运行
[php]
@SET PHPCLI="D:\Program Files\Zend\Zend Studio - 8.0.1\plugins\org.zend.php.debug.debugger.win32.x86_5.3.18.v20110322\resources\php53\php.exe"
设置了PHPCLI的值
[php] view plaincopy
@%PHPCLI% %0
%PHPCLI% 代表的就是你上面设置的路径,这里表示执行php.exe,然后%0代表此.bat文件所在的路径, 连起来的意思就是用php.exe运行.bat文件。
会出现上面现象呢,php可不认为REM是注释呀,只认为是普通的字符。
就会首先打印@REM,然后
[php]
@REM =='
@SET PHPCLI="D:\Program Files\Zend\Zend Studio - 8.0.1\plugins\org.zend.php.debug.debugger.win32.x86_5.3.18.v20110322\resources\php53\php.exe"
@REM PHPCLI 设置你的php.exe的路径
@%PHPCLI% %0
@goto :EOF
@REM';?>
这里,php.exe可认识了,表示上面意思?在php中@可认识否?这个表示抑制错误,那么就是字符串‘’中的内容与REM进行==,那么有被@抑制错误。安然度过。
进入下一段
[php]
include'C:\\Program Files\\Zend\\Apache2\\htdocs\\pic_gather\\pic_operate.php';
echo "\rfinish";
ob_flush();
exec("pause");
?>
这里就是你要执行的程序了。我不想写在这里面,那么就include一下,这样容易让.bat成为一个模板。
到此你的php.exe算是走完了,可是还没完呢,刚刚批处理命令才到
[php]
@%PHPCLI% %0
之后为了不让批处理命令不认识php的语言,用了
[php]
@goto :EOF
来结束程序。
有人问为什么不直接这样:
[php]
@SET PHPCLI="D:\Program Files\Zend\Zend Studio - 8.0.1\plugins\org.zend.php.debug.debugger.win32.x86_5.3.18.v20110322\resources\php53\php.exe"
@REM PHPCLI
@%PHPCLI% %0
@goto :EOF
include'C:\\Program Files\\Zend\\Apache2\\htdocs\\pic_gather\\pic_operate.php';
echo "\rfinish";
ob_flush();
exec("pause");
?>
不用搞这么复杂,我也试了下,
发现cmd命令行会出现:
@SET PHPCLI="D:\Program Files\Zend\Zend Studio - 8.0.1\plugins\org.zend.php.debu
g.debugger.win32.x86_5.3.18.v20110322\resources\php53\php.exe"
@REM PHPCLI
@%PHPCLI% %0
@goto :EOF
这代码的原作者,就是想避免出现这样的情况,然打印出太多的垃圾信息。
所以说这才是原作者的精辟之处呀。
第三种
这个如果你,仅仅只要更新里面的字符串信息,比如你设置了class类,如果class='自然风光',那么就把image前面加上pic_gather/nature/image。那么没有必要用上面的这么复杂的东西,直接用sql语言就行了,concat()函数就有这样的功能。
作者:wolinxuebin

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
