第一次使用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
工作完美,我相信问题有以下原因之一:
-
pthreads
未安装。pthreads
部分检查phpinfo()
在Apache请求中调用
pthreads
。在这种情况下,pthreads
没有按预期工作。请参阅此说明:https://github.com/krakjoe/pthreads SAPI支持。pthreads
只能作为命令行使用。确保这两个点都解决了,然后它就会工作!