Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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); function on another thread and then just continue with execution? What is the best practice for this kind of problem?

like image 707
Xardas Avatar asked Jan 12 '23 14:01

Xardas


1 Answers

PHP does do multithreading, the documentation for pthreads can be found: http://php.net/pthreads

Many examples of usage: 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. 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. 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. 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.

like image 116
Joe Watkins Avatar answered Jan 16 '23 22:01

Joe Watkins