PHP定时器没有脚本暂停/延迟
所以我只想将这个快速而肮脏的模块写入一个程序,该程序获取当前时间,回显一个字符串,然后等待 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以复制正在运行的当前进程,让 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
当然,分叉可以解决问题,但为什么不使用 ticks 功能< /a> 在 PHP 中?
考虑这个例子:
在睡眠时,它将定期调用tick函数,而无需手动调用它。唯一需要注意的是,如果特定操作需要很长时间才能完成(例如数据库查询),则在该操作完成之前不会触发蜱虫。
Ofcourse forking would do the trick, but why not use the ticks functionality in PHP?
Consider this example:
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.