Throttling PHP脚本的建议/技巧 [英] Suggestions/Tricks for Throttling a PHP script

查看:193
本文介绍了Throttling PHP脚本的建议/技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定期运行脚本的计划任务(每小时)。此脚本与数据库和文件系统执行一些重要的交互,并且运行需要几分钟。问题是,当脚本运行时,服务器的cpu使用峰值,并减慢正常操作。有没有办法扼制这个过程,以便它需要更长的时间,但不消耗尽可能多的资源?

I have a scheduled task that runs a script on a regular basis (every hour). This script does some heavy interaction with the database and filesystem and regularly takes several minutes to run. The problem is, the server's cpu-usage spikes while the script is running and slows down normal operations. Is there a way to throttle this process so that it takes longer but does not consume as many resources?

我看过不同的配置选项的PHP,但没有似乎是符合我的需要的任何。

I've looked at different configuration options for PHP but there does not appear to be any that fit my needs.

将php.ini中的memory_limit设置为较低的值会使我的数据对象很容易溢出。

Setting memory_limit in php.ini to something lower causes my data objects to overflow quite easily.

ve看到类似的帖子,人们建议在脚本中的某些点使用sleep(),但不能防止脚本spiking服务器。

I've seen similar posts where people suggested using sleep() at certain points in the script but that does not prevent the script from spiking the server.

最佳解决方案是告诉Lamp(在这种情况下是Wamp)堆栈只使用10%的最大cpu利用率的一些方法。我不关心运行时,并希望它需要更长时间,如果它意味着每秒保存cpu周期。我的替代解决方案将是设置一个不同的服务器与数据库复制,所以cron可以去城镇,而不减缓其他一切。

The optimal solution would be some way to tell the Lamp (in this case Wamp) stack to only use 10% max cpu utilization. I'm not concerned at all about runtime and would prefer that it take longer if it means saving cpu cycles per second. My alternate solution would be to setup a different server with database replication so the cron could go to town without slowing everything else down.

环境:Windows Server 2k3,Apache 2.2。 11,PHP 5.2.9,MySQL 5.1

Environment: Windows Server 2k3, Apache 2.2.11, PHP 5.2.9, MySQL 5.1

我很欣赏这种情况。

EDIT:我感谢所有的答案,甚至那些是*特定的nix。在我的情况下,还是很早就改变了托管环境。希望这个问题将帮助别人不管操作系统。

I appreciate all the answers, even the ones that are *nix-specific. It's still early enough in my situation to change the hosting environment. Hopefully this question will help others out regardless of the OS.

推荐答案

这是一个棘手的问题。如果你通过命令行运行PHP脚本,你可以将进程的调度优先级设置为低( start / low php.exe myscript.php 我相信)。如果你的PHP脚本本身正在做大部分处理你的CPU,这可能工作。但是,你说你正在做一些沉重的数据库和文件系统交互,这个解决方案不会帮助。看起来有一个MySQL提示LOW_PRIORITY的INSERT和UPDATE查询可能会帮助你,但我没有尝试这些。

This is a tricky problem. If you're running the PHP script via the command line, you can set the process's scheduling priority to low (start /low php.exe myscript.php I believe). If your PHP script itself is actually doing most of the processing that's eating your CPU, this might work. However, you said you are doing some heavy database and filesystem interaction, which this solution will not help. It looks like there is a MySQL hint "LOW_PRIORITY" for INSERT and UPDATE queries that may help you there, but I have not tried those.

这篇关于Throttling PHP脚本的建议/技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆