简体   繁体   English

PHP页面执行后台进程

[英]PHP page to execute a background process

I have a PHP script to pull user specific data from a 3rd party source and dump it into a local table, which I want to execute every X mins when a user is logged in, but it takes about 30 seconds to run, which I don't want the user to experience. 我有一个PHP脚本,可从第三方来源提取用户特定的数据并将其转储到本地表中,我想在用户登录时每隔X分钟执行一次,但是运行大约需要30秒,我不这样做不想让用户体验。 I figured the best way of doing this would be to timestamp each successful pull, then place some code in every page footer that checks the last pull and executes the PHP script in the background if it was more than X minutes ago. 我认为最好的方法是为每个成功的请求添加时间戳,然后在每个页脚中放置一些代码,以检查上一次请求,如果超过X分钟前,则在后台执行PHP脚本。

I don't want to use a cron job to do this, as there are global and session variables specific to the user that I need when executing the pull script. 我不想使用cron作业来执行此操作,因为执行pull脚本时需要特定于用户的全局变量和会话变量。

I can see that popen() would allow me to run the PHP script, but I can't seem to find any information relating to whether this would be run as the visitor within their session (and therefore with access to the user specific global or session variables) or as a separate session altogether. 我可以看到popen()允许我运行PHP脚本,但是我似乎找不到任何有关是否以访问者身份在其会话中运行的信息(因此可以访问用户特定的全局变量或会话变量)或作为一个单独的会话。

  1. Will popen() solve my problem? popen()是否可以解决我的问题?
  2. Am I going about this the right way or is there a better method to execute the script at intervals while the user is logged in? 我要采用正确的方法还是在用户登录时间隔执行脚本的更好方法?

Any help or advice is much appreciated, as always! 一如既往,任何帮助或建议都将不胜感激!

Cheers 干杯

Andy 安迪

Maybe an idea to put the session data also in a table. 也许可以将会话数据也放在表中。 That way you can easily access it from your background process. 这样,您可以轻松地从后台进程访问它。 You only have to pass the session_id() or the user id as argument so the process knows which user it is currently processing. 您只需传递session_id()user id作为参数,以便进程知道当前正在处理的用户。

  1. No, because PHP still needs to wait on the process started by popen() before it can exit 不,因为PHP仍然需要等待popen()启动的进程才能退出
  2. Probably not, because the solution doesn't fit the architectural constraints of the system 可能不是,因为该解决方案不适合系统的体系结构约束

Your reasoning for not using a cron job isn't actually sound. 您不使用Cron作业的理由实际上并不合理。 You may have given up on exploring that particular path too quickly and drawn yourself into a corner with trying to fit a square peg in a round hole. 您可能已经放弃了太快地探索该特定路径,而试图将一个方形的钉子插入一个圆孔中而陷入一个角落。

The real problem you're having is figuring out how to do inter-process communication between the web-request and the PHP running from your crontab. 您遇到的真正问题是弄清楚如何在Web请求和从crontab运行的PHP之间进行进程间通信。 This can be solved in a number of ways, and more easily then trying to work against PHP's architectural constraints. 这可以通过多种方式解决,然后可以轻松地克服PHP的体系结构约束。

For example, you can store the session ids in your database or some other storage, and access the session files from the PHP script running in your crontab, independently of the web request. 例如,您可以将会话ID存储在数据库或其他存储中,并独立于Web请求从crontab中运行的PHP脚本访问会话文件。

Let's say you determine that a user is logged in based on the last request they made to your server by storing this information in some data store as a timestamp, along with the current session id in the user table. 假设您是根据用户对服务器的上一次请求确定登录的,方法是将该信息作为时间戳存储在某个数据存储中,并将当前会话ID存储在user表中。

The cron job can startup every X minutes, look at the database or persistence store for any records that have an active timestamp within the given range, pull those session ids and do what's needed. cron作业可以每隔X分钟启动一次,查看数据库或持久性存储中是否有给定范围内具有活动时间戳记的所有记录,提取这些会话ID并执行所需的操作。

Now the question of how do you actually get this to scale effectively if the processing time takes more than 30 seconds can be solved independently as well. 现在,如果处理时间超过30秒,您如何真正有效地进行扩展的问题也可以独立解决。 See this answer for more details on how to deal with distributed job managers/queues from PHP . 有关如何处理来自PHP的分布式作业管理器/队列的更多详细信息,请参见此答案

I would use Javascript and AJAX requests to call the script from the frontend and handle the result. 我将使用Javascript和AJAX请求从前端调用脚本并处理结果。 You could then set the interval in JavaScript and send an AJAX-Request each interval tick. 然后,您可以在JavaScript中设置时间间隔,并在每个时间间隔滴答时发送AJAX请求。

AJAX is what you need: http://api.jquery.com/jquery.ajax/ AJAX是您所需要的: http : //api.jquery.com/jquery.ajax/

The use the method "success" to do something after those 30 seconds. 在那30秒之后,使用方法“成功”执行某项操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM