0

I am using the Facebook SDK to post some things on the timeline. The problem is that it usually takes about 2-3 seconds for the API request and it slows my website, especially since i have a lot of ajax requests.

Is there a way to fork a part of PHP code into the background? I want that all global, session etc to remain the same.

5
  • There's a whole section the PHP manual on Process Control. The functions I am referring to all start with pcntl_. Commented Jun 16, 2012 at 8:52
  • Have you ever tried cron job and curl? Or use ajax trigger to PHP and request these API? Commented Jun 16, 2012 at 9:01
  • i'lld be inclined to use exec to call the script in the background Commented Jun 16, 2012 at 9:02
  • I've understood for a while that PHP wasn't multi-threaded, and that was one of the major reasons some think it's still a junior language (and I'm not arguing one way or the other). Commented Jun 16, 2012 at 9:08
  • Use gearmand and Net_Gearman - nice and reliable, multi-platform, queueing system - and you can spread load onto multiple servers very easily. GearmanManager is also worth a look - find both here. Commented Jun 16, 2012 at 13:24

5 Answers 5

2

You can use PHP exec() to execute a seperate PHP script.

$cmd = "php5 -q /path/to/php/send_fb_request.php";

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

and then if you want to monitor if the process is still running:

function isRunning($pid){
    try{
        $result = shell_exec(sprintf("ps %d", $pid));
        if( count(preg_split("/\n/", $result)) > 2){
            return true;
        }
    }catch(Exception $e){}

    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

As i specified in the question, i need to use the current user session, cookies, initialized classes etc.
Would passing all of the values from your session variables to the script work? php -q /var/somescript.php $_SESSION['ip'] $_SESSION['username']
1

If possible I would create a queue for it and execute those time costing issues from the queue and not from the http process which generates and handles the web requests for your website.

That way you have full control, can separate it even to other servers and it is the most common solution for this kind of issues.

You should preferably use a real queue solution but I have also seen lots of simple MySQL solutions working when projects stay small. Read more about this here: http://www.engineyard.com/blog/2011/5-subtle-ways-youre-using-mysql-as-a-queue-and-why-itll-bite-you/

Checked some of our queues also and it also allows for things like rate limiting, logging and measurements which might occur for several API's. The nice thing is it separates making a post and the real processing with the external party which gives a central point of control and code separation.

Comments

0

Sounds like you're after asynchronous PHP. This article explains a technique quite well: http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html

Comments

0

You can try standalone Symfony2 component Process

Comments

0

Kindly use it , It works for me. It is working awesome.

$pid = pcntl_fork();
if($pid === -1) die('error: unable to fork.');
else if($pid) exit(0);

posix_setsid();
sleep(1);

ob_start();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.