php - Multi-threading in Symfony2

845

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?

815

Answer

Solution:

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.

876

Answer

Solution:

PHP does not support multithreading by default. 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. So what I would recommend and what we do at our company is using a asynchronous messaging tool like 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.

People are also looking for solutions to the problem: php - Unable to load class when on live environment in laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.