简体   繁体   English

在后台执行PHP文件

[英]Execute PHP file in background

I have some php code, that execute for a very long time. 我有一些PHP代码,执行很长一段时间。

I need to realise next scheme: 我需要实现下一个方案:

  1. User enter on some page(page 1) 用户在某些页面上输入(第1页)
  2. This page starts execution of my large PHP script in background .(Every change is writting to database) 这个页面开始在后台执行我的大型PHP脚本。(每次更改都写入数据库)
  3. We sent every N seconds query to database to get current status of execution. 我们将每N秒查询发送到数据库以获取当前执行状态。

I don't want to use exec command because 1000 users makes 1000 php processes. 我不想使用exec命令,因为1000个用户制作了1000个php进程。 It's not way for me... 这不适合我......

Use http requests to the local http server from within your script in combination with phps ignore_client_abort() function. 在脚本中结合ignore_client_abort()函数使用对本地http服务器的http请求。

That way you keep the load inside the http servers worker processes, have a natural limit and queuing of requests comes for free. 这样您就可以将负载保留在http服务器工作进程中,具有自然限制并且请求的排队是免费的。

So you basically want a queue (possibly stored in a database) and a command line script ran by cron that process queued items. 所以你基本上想要一个队列(可能存储在数据库中)和cron运行的命令行脚本来处理排队的项目。

Clarification : I'm not sure about what's unclear about my answer, but this complies with the two requirements imposed by the question: 澄清 :我不确定我的答案有什么不清楚,但这符合问题所强加的两个要求:

  1. The script cannot be aborted by the client 客户端无法中止脚本
  2. You share a single process between 1,000 clients 您在1,000个客户端之间共享一个进程

You can use CLI to execute multiple PHP scripts 您可以使用CLI执行多个PHP脚本

or 要么

you can try Easy Parallel Processing in PHP 您可以在PHP中尝试Easy Parallel Processing

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

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