adoy / PHP-FastCGI-Client

Lightweight, single file, FastCGI client for PHP
MIT License
302 stars 61 forks source link

need example of "async_request" #6

Open phpconnect opened 10 years ago

phpconnect commented 10 years ago

Can you let us know the example of "async_request" model?

langemeijer commented 9 years ago

There is one in the code already. Because all requests are performed asynchronously in the fastcgi protocol. Look at the code below:

public function request(array $params, $stdin)
{
    $id = $this->async_request($params, $stdin);
    return $this->wait_for_response($id);
}

With the current state of this project you could start multiple requests

    $id1 = $this->async_request($params1, $stdin1);
    $id2 = $this->async_request($params2, $stdin2);

And ready the results later:

$result1 = $this->wait_for_response($id1);
$result2 = $this->wait_for_response($id2);

If the $id2 request finishes before the $id1 request, the results are retrieved and returned on the wait_for_response($id2) call.

Sadly there currently is no way to execute code (like a callback or something) as soon a request is ready. Nothing is stopping you to fork, implement and contribute of course.

If you are looking for a non-blocking implementation, follow issue https://github.com/adoy/PHP-FastCGI-Client/issues/12 to see if that is going to happen. The non-blocking socket model requires a very different type of code and as a result will always support in a callback-like model