我的线程脚本没有看到线程中的另一个类。这是我的代码。
require_once __DIR__.'\vendor\autoload.php';
use Symfony\Component\DomCrawler\Crawler;
$threadCount = 5;
$list = range(0,100);
$list = array_chunk($list, $threadCount);
foreach ($list as $line) {
$workers = [];
foreach (range(0,count($line)-1) as $i) {
$threadName = "Thread #".$i.": ";
$workers[$i] = new WorkerThreads($threadName,$line[$i]);
$workers[$i]->start();
}
foreach (range(0,count($line)-1) as $i) {
$workers[$i]->join();
}
}
class WorkerThreads extends Thread {
private $threadName;
private $num;
public function __construct($threadName,$num) {
$this->threadName = $threadName;
$this->num = $num;
}
public function run() {
if ($this->threadName && $this->num) {
$result = doThis($this->num);
printf('%sResult for number %s' . "\n", $this->threadName, $this->num);
}
}
}
function doThis($num){
$response = '<html><body><p class="message">Hello World!</p></body></html>';
$crawler = new Crawler($response);
$data = $crawler->filter('p')->text();
return $data;
}
当我运行它时,我收到以下错误消息
Fatal error: Class 'Symfony\Component\SomeComponent\SomeClass' not found
我如何让我的线程看到另一个类?
答案 0 :(得分:0)
在他删除的@Federkun回答的帮助下,我找到了following thread discussing the issue
这是一个有效的解决方案
$autoloader = require __DIR__.'\vendor\autoload.php';
use Symfony\Component\DomCrawler\Crawler;
$threadCount = 1;
$list = range(0,100);
$list = array_chunk($list, $threadCount);
foreach ($list as $line) {
$workers = [];
foreach (range(0,count($line)-1) as $i) {
$threadName = "Thread #".$i.": ";
$workers[$i] = new WorkerThreads($autoloader,$threadName,$line[$i]);
$workers[$i]->start();
}
foreach (range(0,count($line)-1) as $i) {
$workers[$i]->join();
}
}
class WorkerThreads extends Thread {
private $threadName;
private $num;
public function __construct(Composer\Autoload\ClassLoader $loader,$threadName,$num) {
$this->threadName = $threadName;
$this->num = $num;
$this->loader = $loader;
}
public function run() {
$this->loader->register();
if ($this->threadName && $this->num) {
$result = doThis($this->num);
printf('%sResult for number %s' . "\n", $this->threadName, $this->num);
}
}
}
function doThis($num){
$response = '<html><body><p class="message">Hello World!</p></body></html>';
$crawler = new Crawler($response);
$data = $crawler->filter('p')->text();
return $data;
}