PHP定时器没有脚本暂停/延迟

发布于 2024-09-06 16:29:12 字数 110 浏览 1 评论 0原文

所以我只想将这个快速而肮脏的模块写入一个程序,该程序获取当前时间,回显一个字符串,然后等待 x 分钟并回显另一个字符串。唯一的问题是,这个小模块会停止程序的其余部分,直到完成为止。有什么办法解决这个问题吗?

So I just want to write this quick and dirty module to a program that takes the current time, echos a string, and then waits x minutes and echos another string. The only thing is, is this little module stops the rest of the program until it's finished. Any way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-09-13 16:29:12

您可以复制正在运行的当前进程,让 1 个进程等待,而另一个进程执行它必须执行的操作。

要分叉 PHP 进程,您必须使用 pcntl_fork

You can duplicate the current process on which it is running to leave 1 process waiting and the other one do what it has to do.

To fork a PHP process you have to use pcntl_fork

情绪操控生活 2024-09-13 16:29:12

当然,分叉可以解决问题,但为什么不使用 ticks 功能< /a> 在 PHP 中?

考虑这个例子:

declare(ticks=1);

function tick() {
   // check if the interval is correct before proceding
   $mem = memory_get_usage(true);
   echo "Using $mem bytes right now";
}

register_tick_function('tick');

// do all kinds of things in your main program here
while(true) {
   usleep(50000);
}

在睡眠时,它将定期调用tick函数,而无需手动调用它。唯一需要注意的是,如果特定操作需要很长时间才能完成(例如数据库查询),则在该操作完成之前不会触发蜱虫。

Ofcourse forking would do the trick, but why not use the ticks functionality in PHP?

Consider this example:

declare(ticks=1);

function tick() {
   // check if the interval is correct before proceding
   $mem = memory_get_usage(true);
   echo "Using $mem bytes right now";
}

register_tick_function('tick');

// do all kinds of things in your main program here
while(true) {
   usleep(50000);
}

While sleeping it will periodically call the tick function, without having to invoke it manually. The only caveat is that if a particular operation takes a very long time to complete (e.g. a database query) the ticks will not fire until this operation is complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文