【问题标题】:Access model from Thread in YiiYii 中 Thread 的访问模型
【发布时间】:2013-09-19 08:08:00
【问题描述】:

我必须在 Yii 1.1 应用程序中解析一个巨大的 csv 文件。 必须验证每一行并将其保存到数据库中。 我决定使用多线程来完成这项任务。

这是我在控制器操作中的代码:

public function parseData($) {
        $this->content = explode("\n", $this->content);

        $thread_1 = new DatalogThread(array_slice($this->content, 0, 7000));
        $thread_2 = new DatalogThread(array_slice($this->content, 7001));

        $thread_1->start();
        $thread_2->start();
    }

还有线程(我把它放在模型文件夹中):

class DatalogThread extends Thread {
    public $content;


    public function __construct($content) {
       $this->content = $content;

    }


    public function run() {
       foreach ($this->content as $value) {
            $row = str_getcsv($value);

            $datalog = new Datalog($row);
            $datalog->save();

        }
    }

}

问题是线程无法访问模型文件:

致命错误:在 C:\xampp...\protected\models\DatalogThread.php 中找不到类“Datalog”

我尝试了 Yii::autoload("Datalog"),但得到以下错误:

致命错误:无法在第 402 行访问 ...\YiiMain\framework\YiiBase.php 中的属性 Yii::$_coreClasses

【问题讨论】:

  • “Datalog”类在哪里定义?
  • 确保检查文件名格式和类名是否正确。
  • 类的自动加载是在 Yii 框架中完成的。我确定一切都设置正确,因为如果我在控制器操作中使用 $thread_1->run() 而不是 $thread_1->start() 一切正常,但没有线程功能。

标签: php multithreading yii


【解决方案1】:

Yii 使用了很多的静态代码,这不是多线程的最佳代码。

你想要做的是初始化不知道 Yii 的线程并重新加载它,我不使用 Yii,但这里有一些工作可以让你知道该怎么做:

<?php
define ("MY_YII_PATH", "/usr/src/yii/framework/yii.php");

include (MY_YII_PATH);

class YiiThread extends Thread {
    public $path;
    public $config;

    public function __construct($path, $config = array()) {
        $this->path = $path;
        $this->config = $config;
    }

    public function run() {
        include (
            $this->path);
        /* create sub application here */

    }
}

$t = new YiiThread(MY_YII_PATH);
$t->start(PTHREADS_INHERIT_NONE);
?>

这会更好......我应该认为你想要 yii 在你的线程中调用一个控制台应用程序,因为你不希望它尝试发送任何标题或类似的东西......

这应该让你开始......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多