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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算