douzi8548 2014-08-12 20:11
浏览 62
已采纳

从pthreads访问$ _SESSION变量

I'm having an issue accessing the session variable from functions called from Thread objects using the pthreads library for PHP.

When the function is called from the main thread, no errors occur and everything runs fine.

When run from a Thread object however, I get the following errors:

> PHP Notice:  Undefined variable: _SESSION
> PHP Notice:  Undefined index: Properties Manager
> PHP Fatal error:  Call to a member function getGroupValue() on a non-object

The line numbers specified by the errors all point to this code block:

function connect_mysql_db($database, $write = false) {
    $properties = $_SESSION['Properties Manager'];

    if(!isset($database) || strlen($database)==0){
        throw new Exception("No database specified");
    }

    // Read appropriate host, port, dbname, user & pass for this database
    $host    = $properties->getGroupValue($database, DB_HOST);
    $port    = $properties->getGroupValue($database, DB_PORT);
    $db_name = $properties->getGroupValue($database, DB_NAME);
    ...Removed unnecessary code...
}

A little searching says that I should be able to remedy this issue by putting session_start(); at the top of my file. After doing this, the other errors are still printed in addition to:

> PHP Notice:  A session had already been started - ignoring session_start()

So my main question: Is there something special that I need to do when using pthreads in order to access the super-global session? Or is there something completely different at play here that I am missing?

Edit: Yes, I have tried global $_SESSION; as well.

  • 写回答

1条回答 默认 最新

  • dpquu9206 2014-08-13 15:55
    关注

    After a bit more research, it seems that the session variable is not thread safe and in fact locks entirely until the session is closed. I had to remove references to the session and simply pass the needed information to the function or reinitialize it.

    Luckily there isn't a performance downfall of the property manager being created.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器