简体   繁体   English

Symfony2中的多线程

[英]Multi-threading in Symfony2

I have a bit of situation here. 我在这里有一些情况。 I'm using symfony2 and facebook SDK to set up a web service for my IPhone and Android applications. 我正在使用symfony2和facebook SDK为我的iPhone和Android应用程序设置Web服务。 The problem is that the background work can take quite a while. 问题是后台工作可能需要很长时间。 User is first redirected to facebook login page where he can proceed by accepting my facebook app permissions. 用户首先被重定向到Facebook登录页面,在那里他可以继续接受我的Facebook应用程序权限。 So, instead of waiting for my service to complete background work, user should be notified immediately that everything is in order. 因此,不应等待我的服务完成后台工作,应立即通知用户一切正常。 Service should continue work in background and user should be unaware of it. 服务应该在后台继续工作,用户应该不知道它。 This is the relevant part of controller action: 这是控制器操作的相关部分:

public function persistPostsAction() {

    ...
    if ($this->CheckUser($user_id) == 0) {
/*This function should be called on background thread*/
    $this->persistPosts($user_id);
    }

    ...

    return $this->render('FacebookAPIFacebookBundle:Page:postovi.html.twig', array(
                'FacebookPosts' => $pwu
    ));
}

How can I call $this->persistPosts($user_id); 如何调用$this->persistPosts($user_id); function on another thread and then just continue with execution? 在另一个线程上运行,然后继续执行? What is the best practice for this kind of problem? 这类问题的最佳做法是什么?

PHP does do multithreading, the documentation for pthreads can be found: http://php.net/pthreads PHP确实进行了多线程处理,可以找到pthreads的文档: http//php.net/pthreads

Many examples of usage: https://github.com/krakjoe/pthreads/tree/master/examples 许多用法示例: https//github.com/krakjoe/pthreads/tree/master/examples

It is not as simple as simple create a thread and allow it to execute in the background while you finish servicing the request, while you could do that by detaching from the main thread servicing the request, it's not a very good idea. 它不是简单的创建一个线程并允许它在完成服务请求时在后台执行,而你可以通过从服务请求的主线程中分离来做到这一点,这不是一个好主意。

You never really want to create any threads in direct response to a web request, since this can only scale so far. 您从不真正想要直接响应Web请求创建任何线程,因为这只能扩展到目前为止。 What you want to do instead is separate that part of the application which you require to run constantly and regardless of what the front end parts of the website are doing. 您想要做的事情是将应用程序的一部分分开,您需要不断运行,而不管网站的前端部分是做什么的。 This newly separated part of the application we'll refer to as the back end. 这个新分离的应用程序部分我们将其称为后端。 The back end of the application should be a service that runs all the time, independently of apache, fpm, or nginx. 应用程序的后端应该是一直运行的服务,与apache,fpm或nginx无关。 It very well may use multi threading to maximize throughput of the back end services, you will still need some simple way for the front and back ends to communicate; 它很可能使用多线程来最大化后端服务的吞吐量,你仍然需要一些简单的方法来进行前端和后端的通信; unix domain sockets, tcp sockets etc. With a communication channel between the front and back ends of your application, the front end can pass data and instruct the back end to queue transactions in whatever form is appropriate while the front end never has to wait for a result. unix域套接字,tcp套接字等。通过应用程序的前端和后端之间的通信通道,前端可以传递数据并指示后端以适当的形式排队事务,而前端永远不必等待结果。 This is a much better design, that doesn't necessarily require any multi-threading, although no doubt it's a candidate. 这是一个更好的设计,不一定需要任何多线程,但毫无疑问它是候选者。

It is important to remember that throwing threads at something doesn't necessarily make anything better, the only thing you can say for sure is that it will be busier, performance is not a product of use by default, you have to think carefully about how you will use resources, how you will communicate between the component parts of your application, and how to use the minimum number of threads (or indeed, processes) for the maximum amount of throughput. 重要的是要记住,在某些东西上投掷线程并不一定会让事情变得更好,唯一可以肯定的是,它会更加繁忙,性能不是默认使用的产品,你必须仔细考虑如何您将使用资源,如何在应用程序的组件部分之间进行通信,以及如何使用最少数量的线程(或实际上是进程)来获得最大吞吐量。

PHP does not support multithreading by default. PHP默认情况下不支持多线程。 There are frameworks for PHP like pthreads that bring multi threading into the language but you might not find a lot of documentation for pthreads im combination with symfony2. 像pthreads这样的PHP框架为这种语言带来了多线程,但你可能找不到很多pthreads与symfony2组合的文档。 So what I would recommend and what we do at our company is using a asynchronous messaging tool like RabbitMQ. 所以我推荐的以及我们在公司所做的是使用像RabbitMQ这样的异步消息传递工具。 Basically what you do is send messages from your application which will be stored in a queue and asynchronously processed by worker processes. 基本上,您所做的是从您的应用程序发送消息,这些消息将存储在队列中并由工作进程异步处理。 I can also recommend the videlalvaro bundle for RabbitMQ https://github.com/videlalvaro/rabbitmqbundle . 我还可以为RabbitMQ推荐videlalvaro捆绑包https://github.com/videlalvaro/rabbitmqbundle

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

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