0

I have some PHP script where I invoke method from the external class. I want to run this method asynchronously. I don't want to block rest of the program. This method does some work in the background and return nothing so there is no need to wait while it finished. Is there a way to do this in PHP?

# get post data from user
$postData = $this->f3->get('POST');

# start of asynchronous part
$obj = new asyncClass();
$obj->init($postData);
# end of asynchronous part

# do some work with post data
$soc = new someOtherClass($postData);
$result = $soc->send();
# assign result of work to variable
$this->f3->set('var', $result);
# show it to user
$this->f3->set('view', 'view.html');

If this can help, I'm using Fat Free Framework and PHP 5.6 Non-Thread Safe

2 Answers 2

2

You can use $f3->abort() to send the output/response to the browser and process your other blocking function afterwards. That's not a real asynchron solution but would work. You could also use something like php-icicle to add threads support, but that maybe requires some other php modules being installed.

Sign up to request clarification or add additional context in comments.

Comments

0

Use threading.

class PostDataHandlerAsync extends Thread
{
    private $postData
    public function __construct($postData)
    {
        $this->postData = $postData;
    }

    public function run()
    {
        /*
        Your code goes here
        */
    }
}

$postData = $this->f3->get('POST');
$obj = new PostDataHandlerAsync($postData);
$obj->run();

1 Comment

Thanks for reply. I checked it. PHP version on production server is Non Thread Safe so I'm not able to use threads I guess. I corrected the question.

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.