异步运行 PHP 任务 [英] Run PHP Task Asynchronously

查看:22
本文介绍了异步运行 PHP 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个有点大的 Web 应用程序,后端主要使用 PHP.代码中有几个地方我需要完成一些任务,但我不想让用户等待结果.例如,在创建新帐户时,我需要向他们发送欢迎电子邮件.但是当他们点击完成注册"按钮时,我不想让他们等到真正发送电子邮件,我只想开始这个过程,并立即向用户返回一条消息.

I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don't want to make the user wait for the result. For example, when creating a new account, I need to send them a welcome email. But when they hit the 'Finish Registration' button, I don't want to make them wait until the email is actually sent, I just want to start the process, and return a message to the user right away.

直到现在,在某些地方我一直在使用 exec() 感觉像是一个 hack 的东西.基本上做这样的事情:

Up until now, in some places I've been using what feels like a hack with exec(). Basically doing things like:

exec("doTask.php $arg1 $arg2 $arg3 >/dev/null 2>&1 &");

这似乎有效,但我想知道是否有更好的方法.我正在考虑编写一个在 MySQL 表中排队任务的系统,以及一个单独的长时间运行的 PHP 脚本,该脚本每秒查询一次该表,并执行它找到的任何新任务.如果我需要,这也可以让我将来在几台工作机器之间分配任务.

Which appears to work, but I'm wondering if there's a better way. I'm considering writing a system which queues up tasks in a MySQL table, and a separate long-running PHP script that queries that table once a second, and executes any new tasks it finds. This would also have the advantage of letting me split the tasks among several worker machines in the future if I needed to.

我是在重新发明轮子吗?有比 exec() hack 或 MySQL 队列更好的解决方案吗?

Am I re-inventing the wheel? Is there a better solution than the exec() hack or the MySQL queue?

推荐答案

我使用了排队方法,它工作得很好,因为您可以推迟该处理直到您的服务器负载空闲,让您非常有效地管理您的负载,如果您可以轻松划分不紧急的任务".

I've used the queuing approach, and it works well as you can defer that processing until your server load is idle, letting you manage your load quite effectively if you can partition off "tasks which aren't urgent" easily.

自己动手并不太难,这里有一些其他的选择:

Rolling your own isn't too tricky, here's a few other options to check out:

  • GearMan - 这个答案写于 2009 年,从那时起 GearMan 看起来很受欢迎,请参阅下面的评论.
  • ActiveMQ 如果您想要一个完整的开源消息队列.
  • ZeroMQ - 这是一个非常酷的套接字库,可以轻松编写分布式代码而无需担心关于套接字编程本身的内容太多了.您可以将它用于单个主机上的消息队列 - 您只需让您的 web 应用程序将某些内容推送到一个队列中,连续运行的控制台应用程序将在下一个合适的机会使用该队列
  • beanstalkd - 在写这个答案时才发现这个,但看起来很有趣
  • dropr 是一个基于 PHP 的消息队列项目,但此后一直没有得到积极维护2010 年 9 月
  • php-enqueue 是最近(2017 年)围绕各种队列维护的包装器系统
  • 最后,一篇关于使用 memcached 的博客文章消息队列
  • GearMan - this answer was written in 2009, and since then GearMan looks a popular option, see comments below.
  • ActiveMQ if you want a full blown open source message queue.
  • ZeroMQ - this is a pretty cool socket library which makes it easy to write distributed code without having to worry too much about the socket programming itself. You could use it for message queuing on a single host - you would simply have your webapp push something to a queue that a continuously running console app would consume at the next suitable opportunity
  • beanstalkd - only found this one while writing this answer, but looks interesting
  • dropr is a PHP based message queue project, but hasn't been actively maintained since Sep 2010
  • php-enqueue is a recently (2017) maintained wrapper around a variety of queue systems
  • Finally, a blog post about using memcached for message queuing

另一种可能更简单的方法是使用 ignore_user_abort - 一旦您将页面发送给用户,您可以进行最后的处理而不必担心过早终止,尽管从用户的角度来看,这确实会延长页面加载时间.

Another, perhaps simpler, approach is to use ignore_user_abort - once you've sent the page to the user, you can do your final processing without fear of premature termination, though this does have the effect of appearing to prolong the page load from the user perspective.

这篇关于异步运行 PHP 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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