如何编写在后台运行的异步Web服务php
我正在使用 php 编写后台处理 Web 服务,即使用户关闭浏览器也需要在后台运行,有什么方法可以使用 php 来做到这一点吗?
更多信息
是的,我正在使用 symfony/php 从事大型 Web 项目(工资单)。它需要在每个月工资用户来并单击处理按钮时进行处理。那么工资单应该在没有 apache 服务器超时的情况下处理。为此,我希望编写在后台运行的异步 Web 服务。
im up to write background processing web service using php that need to run in background even user closed the browser is there any way to do this using php ?
More Info
Yes im working on large web project (pay roll) using symfony/php. it needs to process in every month when payroll user comes and click the process button. then payroll should process without apache server time out. for do that i hope to wirte asynchronous web service that run in background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如评论者所说,您应该使用 CRON 作业,因为它最适合此类问题。但是,您需要通过用户单击来启动作业。这是我要使用的:
具有执行任务所需的参数。基本上它说的是
CRON“嘿,你需要启动任务”。
如果找到行/文件,则启动任务。 删除之前的文件
启动任务,或者可能发生并行任务。
任务创建另一个行/文件以显示 CRON 结束,并使用
javascript 每 30 秒/1 分钟刷新一次用户页面,并停止
找到新行/文件时自动刷新,并显示
适当的输出/通知消息。
As commentor said, you should use a CRON job as it's best suited for this kind of problems. However, you need to launch your job on the click of a user. Here is what I'd use:
with needed parameter to execute the task. Basically it says to the
CRON 'hey, you need to launch the task'.
launch the task if the row/file is found. Delete the file before
launching the task, or parallels tasks could happen.
task create another row/file to show that the CRON ended, and with
javascript refresh your user page every 30s/1min, and stop the
automatic refresh when the new row/file is found, and display the
appropriate output/notice message.
看看这篇文章 。根据您正在执行的操作,这可能比 CRON 作业好得多,特别是如果您想立即采取行动。 CRON 作业最多只能运行一次,每分钟一次,通过这种方法,您可以立即开始在后台处理请求。
Take a look at this article. Depending on what you're doing this can be much better than a CRON job, most specifically, if you want to take action immediately. CRON jobs are limited to running at most, once per minute, with this approach, you can begin handling the request in the background immediately.