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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
