2

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

所以这是我在 Controller 操作中的代码:

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

4

1 回答 1

0

Yii uses a LOT of statics, this is not the best kind of code for multi-threading.

What you want to do is initialize threads that are not aware of Yii and reload it, I do not use Yii, but here's some working out to give you an idea of what to do:

<?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);
?>

This will work much better ... I should think you want what yii calls a console application in your threads, because you don't want it trying to send any headers or anything like that ...

That should get you started ...

于 2013-10-03T18:13:36.320 回答