doru52911 2014-07-31 13:50
浏览 64
已采纳

在后台执行PHP MYSQL [关闭]

Is it possible, on a website/webserver (having full root access) to run a PHP script which calls mysql queries in the background. What I mean with that:

An user clicks to process something - however to prevent the user waiting for running the query it should look like it is done for the user - he doesn't has to wait for the PHP/MYSQL in the browser However the script should be running on the server and finish

How can I do that? If there is none effective solution in PHP - is it possible with other languages?

I'm not talking about cron jobs - I'm on a ubuntu machine (no windows)

Would be for running many PHP scripts (all the same) in the background - Nginx be the better solution or Apache? Is it even relevant?

  • 写回答

1条回答 默认 最新

  • dongpi9480 2014-07-31 13:58
    关注

    The best architecture I could recommend here is probably a queue/worker setup. For instance, this is simple to do with Gearman (alternatively: ØMQ, RabbitMQ or similar advanced queues). You spin up a number of workers which run in the background and can handle the database query (I'm partial to daemonizing them with supervisord). Spin up as many as you want to support running in parallel; since the job is apparently somewhat taxing, you want to carefully control the number of running workers. Then any time you need to run this job, you fire off an asynchronous job for the Gearman workers and then return immediately to your user. The workers will handle the request whenever they get around to do it. This assumes you don't need any particular feedback for the user, that the job can simply finish whenever without anybody needing to know about it immediately.

    If you do need to provide user feedback when the job is finished, you may simply want to try to execute the request via AJAX. For really sophisticated setups and realtime feedback, you may use the Gearman approach with feedback delivered via a pub/sub websocket. That's quite an involved setup though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器