Pthreads等待线程函数完成后继续运行


pthreads waiting thread function to finish before move on

第一次使用pthreads。我已经创建了一个类,它扩展了线程,并像往常一样从另一个类调用它。

下面是我的代码:
<?php
class TestThread extends Thread
{
    //
    function __construct($parameters) {
        $this->reverse = $parameters;
    }   
    public function run() {
        try {
            sleep(20);
            // Connecting to mysql database
            $cn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
            INSERT INTO DEVICE_SESSION_ADDRESS (X, Y, Z)
                VALUES (1,2,3)   
        } catch (Exception $ex) {
           //do stuffs
        } 
    }       
}
?>

我从一个Rest api方法调用这个函数:

 function test ($response) {
    require_once 'TestThread.php';
    $rev = new TestThread($response);
    $rev->start();
 }

使用Postman测试rest api,它在释放调用之前等待20秒。函数工作,它插入到数据库中。

我能做些什么来解决它,并异步调用我的类TestThread ?

由于pthreads工作完美,我相信问题有以下原因之一:

  1. pthreads未安装。pthreads部分检查phpinfo()
  2. 在Apache请求中调用pthreads。在这种情况下,pthreads没有按预期工作。请参阅此说明:https://github.com/krakjoe/pthreads SAPI支持pthreads只能作为命令行使用。

确保这两个点都解决了,然后它就会工作!