continue processing php after sending http response

后端 未结 12 1063
旧时难觅i
旧时难觅i 2020-11-22 13:06

My script is called by server. From server I\'ll receive ID_OF_MESSAGE and TEXT_OF_MESSAGE.

In my script I\'ll handle incoming text and ge

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 13:22

    There is another approach and its worthwhile considering if you don't want to tamper with the response headers. If you start a thread on another process the called function wont wait for its response and will return to the browser with a finalized http code. You will need to configure pthread.

    class continue_processing_thread extends Thread 
    {
         public function __construct($param1) 
         {
             $this->param1 = $param1
         }
    
         public function run() 
         {
            //Do your long running process here
         }
    }
    
    //This is your function called via an HTTP GET/POST etc
    function rest_endpoint()
    {
      //do whatever stuff needed by the response.
    
      //Create and start your thread. 
      //rest_endpoint wont wait for this to complete.
      $continue_processing = new continue_processing_thread($some_value);
      $continue_processing->start();
    
      echo json_encode($response)
    }
    

    Once we execute $continue_processing->start() PHP wont wait for the return result of this thread and therefore as far as rest_endpoint is considered. It is done.

    Some links to help with pthreads

    • How can one use multi threading in PHP applications
    • http://php.net/manual/kr/pthreads.installation.php

    Good luck.

提交回复
热议问题