【发布时间】: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