【问题标题】:PHP pthreads and SQLite3PHP pthreads 和 SQLite3
【发布时间】:2015-02-28 15:20:33
【问题描述】:

更新

我发现这不是 SQLite 问题。
看看这个

代码1

<?php
class Test
{
    protected $member;
    public function __construct($member = null)
    {
        $this->member = $member;
        $this->member->text = 'changed text';
    }
}
class Text
{
    public $text = 'this is text';
}
$text = new Text();
$test = new Test($text);
echo $text->text.PHP_EOL;

输出1

changed text

代码2

<?php
class Test extends Thread  // Difference
{
    protected $member;
    public function __construct($member = null)
    {
        $this->member = $member;
        $this->member->text = 'changed text';
    }
}
class Text
{
    public $text = 'this is text';
}
$text = new Text();
$test = new Test($text);
echo $text->text.PHP_EOL;

输出2

this is text

并且启用了 php 线程安全。(来自 phpinfo)
这是php故障吗?

原创

我在使用 SQLite3in php 时遇到了一些线程问题。
代码很简单

<?php
class Test
{
    protected $db;
    public function __construct()
    {
        $this->db = new SQLite3(__DIR__.'/test.db');
        echo $this->db->lastErrorCode().PHP_EOL;
    }
}
$test = new Test();

output :0

上面的代码很好用。
但是下面的代码不起作用,它只是扩展了 Thread

<?php
class Test extends Thread
{
    protected $db;
    public function __construct()
    {
        $this->db = new SQLite3(__DIR__.'/test.db');
        echo $this->db->lastErrorCode().PHP_EOL;
    }
}
$test = new Test();

output : PHP Warning: SQLite3::lastErrorCode(): The SQLite3 object has not been proper initialized in /home/ordinaryparksee/Projects/human/test.php on line 8 警告:SQLite3::lastErrorCode(): SQLite3 对象未在第 8 行的 /home/ordinaryparksee/Projects/human/test.php 中正确初始化

怎么了??

【问题讨论】:

  • 不是我的领域,但我猜Thread 需要更多关注,比如你需要实现的方法?
  • 你的 sqlite3 是否启用了-DTHREADSAFE
  • 亲爱的@mario。是的,我使用选项--enable-threadsafe 重新构建 sqlite3。当 make 脚本编译时,我检查了-DSQLITE_THREADSAFE=1。但它仍然无法正常工作。

标签: php pthreads


【解决方案1】:

发件人:https://stackoverflow.com/a/14565559/3333776

在介绍页面的手册中,它指出任何对象 旨在由多个上下文操作的应扩展 Stackable、Thread 或 Worker,例如

<?php
class Test extends Thread  // Difference
{
    protected $member;
    public function __construct($member = null)
    {
        $this->member = $member;
        $this->member->text = 'changed text';
    }
}
class Text extends Threaded  // Difference2
{
    public $text = 'this is text';
}
$text = new Text();
$test = new Test($text);
echo $text->text.PHP_EOL;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多