是否可以优化 Php 脚本以限制对服务器内存和 cpu 的影响?
我有一个 PHP 脚本通过 cronjob 在我的服务器上运行。该作业每分钟运行一次。在 php 脚本中,我有一个执行循环,然后等待一秒并再次循环。本质上是创建一个每秒运行一次的脚本。
现在我想知道,如果我让 cronjob 每小时只运行一次,并且让脚本仍然循环一整小时或可能一整天..这会对服务器 cpu 和/或内存产生任何影响吗?如果是的话,会它是积极的还是消极的?
I have a PHP-script running on my server via a cronjob. The job runs every minute. In the php script i have a loop that executes, then waits one sevond and loops again. Essentially creating a script to run once every second.
Now I'm wondering, if i make the cronjob run only once per hour and have the script still loop for an entire hour or possible an entire day.. Would this have any impact on the servers cpu and or memory and if so, will it be positive or negative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了一个设计缺陷。
您始终可以让 PHP 脚本在循环中永久运行,执行您需要的任何功能,而不依赖于网络服务器或客户端。
显然你正在用这个脚本检查一些东西,有什么煽动吗?也许有更好的解决方案适合您。例如,如果它是数据库,请考虑 SQL 触发器。
I spot a design flaw.
You can always have a PHP script permanently running in a loop performing whatever functionality you require, without dependency upon a webserver or clients.
You are obviously checking something with this script, any incites into what? There may be better solutions for you. For example if it is a database consider SQL triggers.
在我看来,这会产生负面影响。因为脚本不断使用资源。
cron 是根据已在服务器上运行的基于时间的比例来调用的。
但cronjob最多只能每分钟运行一次。
另一件事是,如果脚本超时、失败、崩溃,无论出于何种原因,您最终都不会运行脚本最多一小时。会对服务器负载产生积极影响,但我猜这不是您想要的? :)
也许每 2 甚至 5 分钟运行一次以减轻服务器负载?
或者也许更改脚本,这样它就不会等待,而只执行一次并从 cron 作业调用它。应该会对服务器负载产生积极的影响。
In my opinion it would have a negative impact. since the scripts keeps using resources.
cron is called on a time based scale that is already running on the server.
But cronjob can only run once a minute at most.
Another thing is if the script times out, fails, crashes for whatever reason you end up with not running the script for at max one hour. Would have a positive impact on server load but not what you're looking for i guess? :)
maybe run it every 2 or even 5 minutes to spare server load?
OR maybe change the script so it does not wait but just executes once and calling it from cron job. should have a positive impact on server load.
我认为如果可能的话您应该更改脚本逻辑。
如果您的脚本执行的任务不是周期性的,而是由某些事件触发的,您可以使用一些消息队列(例如Gearman) 。
否则你的解决方案就可以了。可能会发生内存泄漏,但在新的 PHP 版本 (5.3.x) 中,垃圾收集器非常好。某些扩展可能会导致内存泄漏。或者您的应用程序设计可能会导致大量内存使用(例如 Doctrine ORM 加载对象缓存)。
但是您可以通过 monit 等工具控制脚本内存使用情况,并在内存限制达到某个点或启动脚本时重新启动脚本当您的脚本意外关闭时再次执行。
I think you should change script logic if it is possible.
If tasks your script executes are not periodic but are triggered by some events, the you can use some Message Queue (like Gearman).
Otherwise your solution is OK. Memory leaks can occurs, but in new PHP versions (5.3.x) Garbage Collector is pretty good. Some extensions can lead to memory leaks. Or your application design can lead to hungry memory usage (like Doctrine ORM loaded objects cache).
But you can control script memory usage by tools like monit and restart your script when mempry limit reaches some point or start script again when your script unexpectedly shuts down.