Home  >  Article  >  Backend Development  >  swoole http server implements the jump function, and the program does not need to continue execution.

swoole http server implements the jump function, and the program does not need to continue execution.

WBOY
WBOYOriginal
2016-10-11 14:23:331726browse

Use swoole_http_response method header, status, end to achieve jump

The code is as follows

  public function redirect($url)
    {
        $this->header("Location", $url);
        $this->status(302);
        return $this->end('');
    }

But after using redirect(), the program is not interrupted and continues to execute,

Later, I interrupted the program by throwing an exception to prevent it from continuing to execute, but this method caused the worker to restart,

Question: Is there a good way to make the program interrupt immediately after the page jumps? It will not continue to execute and the page will jump immediately? It is required not to use methods that throw exceptions to interrupt the program

Reply content:

Use swoole_http_response method header, status, end to achieve jump

The code is as follows

  public function redirect($url)
    {
        $this->header("Location", $url);
        $this->status(302);
        return $this->end('');
    }

But after using redirect(), the program is not interrupted and continues to execute,

Later, I interrupted the program by throwing an exception to prevent it from continuing to execute, but this method caused the worker to restart,

Question: Is there a good way to make the program interrupt immediately after the page jumps? It will not continue to execute and the page will jump immediately? It is required not to use methods that throw exceptions to interrupt the program

I originally wanted to say exit(0), but it felt a little wrong

I have never used swoole, I just looked at the swoole documentation,

swoole_http_response->end
Send the HTTP response body and end the request processing.
swoole_http_response->end(string $html);
After the end operation, the HTML content will be sent to the client browser and the $request/$response object will be destroyed.
If KeepAlive is turned on, the connection will be maintained and the server will wait for the next time Request
KeepAlive is not turned on, the server will cut off the connection

After

end(), the $response object is destroyed, the http response is completed, and the business code should not be executed.

http://wiki.swoole.com/wiki/p...
It can be understood that reactor is nginx and worker is php-fpm. The reactor thread processes the network request asynchronously and in parallel, and then forwards it to the worker process for processing. The reactor and worker communicate through IPC.

Swoole's reactor, worker, and task_worker can be closely combined to provide more advanced usage.

A more common metaphor, assuming that the server is a factory, then the reactor is sales, helping you receive project orders. The worker is the worker. When the salesperson receives the order, the worker goes to work to produce what the customer wants. Task_worker can be understood as an administrative staff, which can help workers do some chores so that workers can concentrate on their work.

Restarting woker seems to have no effect.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn