我使用Pthread和PHP 7构建了一个TermWorker类的Pool。我从数据库源中选择了几个术语,并且我想用特定的Jobs来处理它。这是我的代码,这样做的:使用Pthread/PHP从作业获取Worker数据
//index.php
$termpool = new Pool(1, TermWorker::class);
foreach($terms as $term) {
$termpool->submit(new PostagJob(["termArray" => $termArray, "lamachinePath" => $lamachine_path]));
$termpool->submit(new ClassifyJob($termArray));
}
在我想将数据保存到工人的PostagJob, 我用一个阵列合并对于这一点,和我设置状态,请参阅:
//PostagJob.php
public function run() {
$this->worker->postag_labels = array_merge(
$this->worker->postag_labels,
//Prevent Volatile object, cast to array
(array)[
"word" => $word_key,
"label" => $word["pos"]["@attributes"]["head"],
"lemma" => $word["pos"]["@attributes"]["class"]
]);
$this->worker->postag_status = "Tagged";
}
在另一个名为ClassifyJob的作业中,我做了几个分类, ,但我也想使用保存的postag_labels(来自worker)。 目前我使用一个while循环来检查数据:
//ClassifyJob.php
public function run() {
while(true) {
//Postag labels are available
if($this->worker->postag_status === "Tagged") {
print_r($this->worker->postag_labels);
break;
} else if($this->worker->postag_status === "Untaggable") {
//Not possible, to classify this way
break;
}
}
}
我注意到,没有被打印的数据,并且它一直等待/循环。 如何从ClassifyJob中的工作人员中检索数据? 我看到了synchronized()
,wait()
和join()
,那是我需要的吗? 我该如何实现这一点,或者我还需要做什么?
编辑:我的完整代码是在此Github上的回购:https://github.com/KSART-nl/terma。