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

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

问题描述

我在有些大型Web应用程序的工作,和后端主要是在PHP。有在code几个地方,我需要完成一些任务,但我不想让用户等待结果。例如,创建一个新的帐户时,我需要向他们发送欢迎电子邮件。但是,当他们打完成注册按钮,我不想让他们等到邮件实际发送,我只是想启动这一进程,并返回一个信息给用户的时候了。

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一个黑客()。基本上做这样的事情:

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更好的解决方案()黑客或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 - 这是一个pretty酷套接字库,这使得它很容易编写分布式code,而不必过分担心的套​​接字编程本身。你可以用它在一台主机上的消息队列 - 你只会有你的web应用推东西的队列连续运行控制台应用程序会消耗在下一个合适的机会

  • beanstalkd - 只发现这一个在写这个答案,但看起来有趣

  • dropr 是一个基于PHP的消息队列项目,但并没有因为2010年9月一直积极维护

  • 最后一篇博客文章中有关使用 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
  • Finally, a blog post about using memcached for message queuing

另外,也许是简单的,方法是使用 ignore_user_abort - 一旦你发送的页面给用户,你可以做你的最终处理,而不用担心premature终止,虽然这确实有出现从用户的角度来看延长页面加载的效果。

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天全站免登陆