避免PHP执行时间限制
我需要用 PHP 语言创建一个脚本来执行数字排列。但 PHP 的执行时间限制设置为 60 秒。我怎样才能运行脚本,以便在需要运行超过60个sesunde时,不被服务器中断。我知道我可以更改 php 中的最大执行时间限制,但我想听到另一个不需要提前知道脚本执行时间的版本。
一位朋友建议我经常从服务器登录和注销,但我不知道该怎么做。
欢迎任何建议。示例代码会很有用。
谢谢。
首先我需要输入一个数字,比如说 25。此后脚本启动,它需要执行以下操作:对于每个 <= 25 的数字,它将创建一个文件,其中包含当前阶段生成的数字;对于下一个数字,它将打开先前创建的文件,并将根据打开的文件的行创建另一个文件,依此类推。因为这需要很长时间,所以我需要避免脚本被服务器中断。
I need to create a script in PHP language which performs permutation of numbers. But PHP has an execution time limit set to 60 seconds. How can I run the script so that if you need to run more than 60 sesunde, not been interrupted by the server. I know I can change the maximum execution time limit in php, but I want to hear another version that does not require to know in advance the execution time of a script.
A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.
Any advice is welcome. An example code would be useful.
Thanks.
First I need to enter a number, lets say 25. After this the script is launch and it need to do the following: for every number <= than 25 it will create a file with the numbers generated at the current stage; for the next number it will open the previuos created file, and will create another file base on the lines of the opened file and so on. Because this take to long, I need to avoid the script beeing intrerupted by the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@emanuel:
我想当你的朋友告诉你“一个朋友建议我经常从服务器登录和注销,但我不知道如何做到这一点。”,他/她的意思一定是“将你的脚本计算分成x 件工作并单独运行它”
例如,使用此脚本,您可以执行它 150 次以达到 150! (因式分解)并显示结果:
// 脚本名称:calc.php
BTW (@Davmuz),您只能在 Apache 服务器上使用 set_time_limit() 函数,它在 Microsoft IIS 服务器上不是有效函数。
@emanuel:
I guess when your friend told you "A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.", he/she must have meant "Split your script computation into x pieces of work and run it separately"
For example with this script you can execute it 150 times to achieve a 150! (factorising) and show the result:
// script name: calc.php
BTW (@Davmuz), you can only use set_time_limit() function on Apache servers, it's not a valid function on Microsoft IIS servers.
您可以尝试将想要进行的调用放入队列中,当操作完成时将其序列化到文件(或内存缓存?)。然后,您可以使用 CRON 守护程序每 60 秒执行一次该队列,以便它继续执行工作并完成任务。
这种方法的缺点是添加到队列、文件锁定等方面的问题,如果您立即需要结果,这可能会很麻烦。如果您向数据库添加内容,可能会成功。而且,它的效率也不是很高。
You could try to put the calls you want to make in a queue, which you serialize to a file (or memory cache?) when an operation is done. Then you could use a CRON-daemon to execute this queue every sixty seconds, so it continues to do the work, and finishes the task.
The drawbacks of this approach are problems with adding to the queue, with file locking and the such, and if you need the results immediately, this can prove troublesome. If you are adding stuff to a Db, it might work out. Also, it is not very efficient.
使用 set_time_limit(0) 但您必须禁用安全模式:
http://php.net/manual/en/function.set-时间限制.php
我建议使用固定时间(set_time_limit(300)),因为如果脚本中存在问题(无限循环或内存泄漏),这不会成为问题的根源。
Web 服务器和 Apache 一样,也有 300 秒的最大时间限制,因此您必须更改它。如果你想做一个 Comet 应用程序,最好选择另一个 Web 服务器而不是 Apache,因为它的请求时间可能很长。
如果重型算法需要较长的执行时间,还可以实现并行处理:http://www.google.com/#q=php+parallel+processing
或者使用 cron 或其他任何其他外部脚本来存储输入数据和计算机。
Use set_time_limit(0) but you have to disable the safe_mode:
http://php.net/manual/en/function.set-time-limit.php
I suggest to use a fixed time (set_time_limit(300)) because if there is a problem in the script (endless loops or memory leaks) this can not be a source of problems.
The web server, like Apache, have also a maximum time limit of 300 seconds, so you have to change it. If you want to do a Comet application, it may be better to chose another web server than Apache that can have long requests times.
If you need a long execution time for a heavy algorithm, you can also implement a parallel processing: http://www.google.com/#q=php+parallel+processing
Or store the input data and computer with another external script with a cron or whatever else.