我在Symfony 2.3项目中有一个类正在做一些http请求,需要一些时间。
我想将此任务作为后台进程运行,以便服务器向客户端返回一个答案,后台进程将继续运行。
你知道如何在Symfony中做到这一点?
我发现过程组件: http : //symfony.com/doc/current/components/process.html但我不确定是否可以从那里运行类方法。
I have a class in my Symfony 2.3 project that is doing some http requests and takes some time.
I would like to run this task as a background process, so that the server returns an answer to the client and the background process continues running.
Do you know how to do that in Symfony?
I found the Process Component: http://symfony.com/doc/current/components/process.html but I am not sure if I can run a class method from there.
最满意答案
一个简单的方法是通过使用一个队列和一个symfony命令来处理队列,从而将繁重的工作与响应分开。
http://symfony.com/doc/current/components/console/introduction.html
创建一个处理添加到队列中的作业的symfony命令,然后将要完成的工作添加到控制器的队列中。 该队列可能会作为作业的数据库表来实施。
这样,您可以将成功响应返回给用户,并定期在服务器上运行cron作业,以处理您所需的工作。
A simple way to do this is to separate the heavy lifting from the response by using a queue and a symfony command to process the queue.
http://symfony.com/doc/current/components/console/introduction.html
Create a symfony command that processes the jobs added to a queue, then add the work to be done to the queue from your controller. The queue will probably be implemented as a database table of jobs.
That way you can return a success response to the user and run a cron job on the server regularly to process the work you require.
更多推荐
发布评论