PHP如何在不等待包含结果的情况下继续执行代码 [英] PHP how to have code continue without waiting for results of include

查看:438
本文介绍了PHP如何在不等待包含结果的情况下继续执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这里有一个简单的解决方案:

I'm hoping there is a simple solution here:

我的代码基本上是

....some code.... //populates a MySQL table

include 'run_this_page.php'; //uses data from table above and takes ten minutes to run

....lots more code.... ///also uses data from same table

由于include需要十分钟的执行时间,因此我希望将其启动,然后立即继续进行下一步.接下来的步骤不需要include中的任何内容.我可以想到几种非优雅的方法(创建一个txt文件,该文件将在Windows中触发计划的任务,而不是使用include等)来实现这一目标,是否有更优雅的方法?

As the include takes ten minutes to excute, I'd like for it to be initiated and then move on immediately to the next step. Nothing in the include is required for these next steps. I can think of several non-elegant (create a txt file which will trigger a scheduled task in Windows rather than using include etc) ways to achieve this, is there a more elegant method?

* 更新 *

*Update*

由于脚本的结构,很多人在礼貌地攻击我.让我再解释一下目的是什么

I'm being attacked (politely) by many over the structure of my script. Let me explain a bit more what the purpose is:

1-从第三方API收集原始数据,通过脚本进行过滤并将其存储在数据库中.由于该API的限制,我必须将请求限制为小块,这很容易花费20分钟以上.从这些数据中,我正在寻找两个不同的结果集.这些集合中的每一个都存储在其自己的表中.很快就会在十几个表中增加到十几个结果集. (我只能使用一个表,但这对我们的讨论没有影响.)

1 - Raw data is gathered from a 3rd party API, filtered via the script and stored in the database. Because of limitations in this API, I have to restrict the requests to small chunks and this can easily take over 20 minutes. From this data I am looking for two distinct result sets. Each one of these sets are stored in its own table. This will soon grow to a dozen result sets in a dozen tables. (I could use only one table, but it makes no difference for our discussion).

2-现在有了结果集,我有两个截然不同的脚本,可以分别收集每个脚本的数据.因为目标第三方站点完全不同,所以我在自己的文件中编写了脚本,只是希望它们现在都可以运行.每个过程可能需要十分钟以上.

2 - Now that I have the result sets, I have two distinct scripts that go out and gather data about each. Because the target 3rd party sites are completely different I wrote the scripts in their own files and simply want them to both run now. Each can take upwards of ten minutes.

我希望这个解释对您有帮助

I hope this explanation helps

推荐答案

异步加载PHP的简单方法是仅使用jQuery和AJAX.

A simple method to load PHP asynchronously is to just use jQuery and AJAX.

<? // php code you want to run 1st ?>
<div id="content"></div>
<script>
$( "#content" ).load( "example.php" ); // php to run 3rd
</script>
<? // php code you want to run 2nd ?>

或...

如果要按需加载example.php,则更可靠的解决方案是.

A more robust solution, if you want to load example.php on demand, would be to run a cron job programmatically when the user clicks a 'process data' button on that page.

然后,您可以每60秒使用AJAX对服务器进行一次ping操作,直到数据被更新并使用上面的相同代码将其拉入.

Then you could use AJAX to ping the server every 60 seconds until the data is updated and pull it in using the same code from above.

或...

php执行后台进程

这篇关于PHP如何在不等待包含结果的情况下继续执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆