php 如何使php函数每5秒循环一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33185302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make a php function loop every 5 seconds
提问by Bido262
I am tyring to make a php function that updates every second using php itself no other languages, just pure PHP codes.
我很想制作一个每秒更新一次的 php 函数,使用 php 本身没有其他语言,只有纯 PHP 代码。
function exp(){
//do something
}
I want it to return a value each second. Like update every second.
我希望它每秒返回一个值。喜欢每秒更新一次。
采纳答案by Firas Rassas
It's not preferable to make this in PHP, try to make on client side by calculating difference between time you got from database and current time. you can make this in JS like this:
最好不要在 PHP 中进行此操作,尝试通过计算从数据库中获得的时间与当前时间之间的差异在客户端进行制作。你可以像这样在JS中做到这一点:
setInterval(function(){
// method to be executed;
},5000); // run every 5 seconds
回答by Maz
For an application server (not a web server), best practice is to use an event loop pattern instead of sleep. This gives you the ability to run multiple timers should the need arise (sleep is blocking so nothing else can run in the mean time). Web servers on the other hand should not really be executing any long-running scripts.
对于应用程序服务器(不是 Web 服务器),最佳实践是使用事件循环模式而不是睡眠模式。这使您能够在需要时运行多个计时器(睡眠被阻塞,因此在此期间没有其他任何东西可以运行)。另一方面,Web 服务器不应该真正执行任何长时间运行的脚本。
Whilst other languages give you event loops out of the box (node / js for example, with setInterval), PHP does not, so you have to either use a well known library or make your own). React PHP is a widely used event loop for PHP.
虽然其他语言为您提供开箱即用的事件循环(例如 node/js,使用 setInterval),但 PHP 不会,因此您必须使用众所周知的库或创建自己的库)。React PHP 是一个广泛使用的 PHP 事件循环。
Here is a quick-and-dirty "hello world" implementation of an event loop
这是一个快速而肮脏的“hello world”事件循环实现
define("INTERVAL", 5 ); // 5 seconds
function runIt() { // Your function to run every 5 seconds
echo "something\n";
}
function checkForStopFlag() { // completely optional
// Logic to check for a program-exit flag
// Could be via socket or file etc.
// Return TRUE to stop.
return false;
}
function start() {
$active = true;
$nextTime = microtime(true) + INTERVAL; // Set initial delay
while($active) {
usleep(1000); // optional, if you want to be considerate
if (microtime(true) >= $nextTime) {
runIt();
$nextTime = microtime(true) + INTERVAL;
}
// Do other stuff (you can have as many other timers as you want)
$active = !checkForStopFlag();
}
}
start();
In the real world you would encapsulate this nicely in class with all the whistles and bells.
在现实世界中,您可以在课堂上用所有的口哨声和铃声很好地封装这一点。
Word about threading:
关于线程的词:
PHP is single threaded under the hood (any OS threading must be manually managed by the programmer which comes with a significant learning curve). So every task in your event loop will hold up the tasks that follow. Node on the other hand, for example manages OS threads under the hood, taking that "worry" away from the programmer (which is a topic of much debate). So when you call setInterval(), the engine will work its magic so that the rest of your javascript will run concurrently.
PHP 在幕后是单线程的(任何操作系统线程都必须由程序员手动管理,这具有显着的学习曲线)。因此,您的事件循环中的每个任务都将阻止接下来的任务。另一方面,例如,Node 在幕后管理 OS 线程,从而消除程序员的“担忧”(这是一个备受争议的话题)。因此,当您调用 setInterval() 时,引擎将发挥其魔力,以便您的 javascript 的其余部分将同时运行。
Quick final note: It could be argued that this pattern is overkill if all you want to do is have a single function do something every 5 seconds. But in the case where you start needing concurrent timers, sleep() will not be the right tool for the job.
简短的最后说明:如果您只想让一个函数每 5 秒执行一次操作,那么这种模式可能会有些过分。但是在您开始需要并发计时器的情况下, sleep() 将不是适合该工作的工具。
回答by WisdmLabs
function exp(){
//do something
}
while(true){
exp();
sleep(5);
}
Use sleep function to make execution sleep for 5 seconds
使用 sleep 函数让执行休眠 5 秒
it will be better if you use setInterval and use ajax to perform your action
如果您使用 setInterval 并使用 ajax 来执行您的操作会更好
回答by mertyildiran
sleep()
function is the function that you are looking for:
sleep()
function 是您正在寻找的功能:
while (true) {
my_function(); // Call your function
sleep(5);
}
- While loop with always true
- Call your function inside while loop
- Wait for 5 seconds(sleep)
- Return the beginning of the loop
- While 循环始终为真
- 在 while 循环中调用您的函数
- 等待5秒(睡眠)
- 返回循环的开始
By the way it's not a logical use case of endless loops in PHP if you are executing the script through a web protocol(HTTP, HTTPS, etc.) because you will get a timeout. A rational use case could be a periodic database updater or a web crawler.
顺便说一句,如果您通过 Web 协议(HTTP、HTTPS 等)执行脚本,则它不是 PHP 中无限循环的逻辑用例,因为您将获得timeout。一个合理的用例可以是定期数据库更新程序或网络爬虫。
Such scripts can be executed through command line using php myscript.php
or an alternative (but not recommended) way is using set_time_limitto extend the limit if you insisting on using a web protocol to execute the script.
如果您坚持使用 Web 协议来执行脚本,则可以通过命令行 usingphp myscript.php
或替代(但不推荐)方式使用set_time_limit扩展限制来执行此类脚本。
回答by Anas Km
Suppose exp() is your function
假设 exp() 是你的函数
function exp(){
//do something
}
Now we are starting a do-while loop
现在我们开始一个 do-while 循环
$status=TRUE;
do {
exp(); // Call your function
sleep(5); //wait for 5 sec for next function call
//you can set $status as FALSE if you want get out of this loop.
//if(somecondition){
// $status=FALSE:
//}
} while($status==TRUE); //loop will run infinite
I hope this one helps :)
我希望这个有帮助:)