PHP Pthreads无法将值传递给线程中的子对象
所以,我有大约100多个进程,我想让它们并行执行。我正在使用PHPPThreads来实现这一点 我遇到的问题是线程启动,但它们不会将值传递给在线程内初始化的另一个对象。我想做的是在单独的对象中执行每个子进程 更新代码PHP Pthreads无法将值传递给线程中的子对象,php,multithreading,pthreads,Php,Multithreading,Pthreads,所以,我有大约100多个进程,我想让它们并行执行。我正在使用PHPPThreads来实现这一点 我遇到的问题是线程启动,但它们不会将值传递给在线程内初始化的另一个对象。我想做的是在单独的对象中执行每个子进程 更新代码 我想这是你的问题: $threads[$i]->start(PTHREADS_INHERIT_NONE); 您可能需要能够访问类ChildProcess 由于zend_堆错误(我无法重现):您使用的是哪个版本的PHP和pthreads?我认为这是您的问题: $thread
我想这是你的问题:
$threads[$i]->start(PTHREADS_INHERIT_NONE);
您可能需要能够访问类ChildProcess
由于zend_堆错误(我无法重现):您使用的是哪个版本的PHP和pthreads?我认为这是您的问题:
$threads[$i]->start(PTHREADS_INHERIT_NONE);
您可能需要能够访问类ChildProcess
由于zend_堆错误(我无法重现):您使用的是哪个版本的PHP和pthreads?最后我找到了答案。但我不太确定这是否是一种正确的行为。欢迎您的建议
我所做的是创建一个类变量$this->e
,并将其分配给子进程对象$this->e=new$object
,然后运行init()
方法。init方法返回一些查询作为类成员变量,即$this->e->querys
将这些查询分配给它。我发现的解决方案是,我应该在方法中使用它作为一个单独的变量,而不是作为类变量,一旦我有了查询,我可以直接将它分配给类变量,并在执行$thread->join()
之后调用它,就像$thread->querys
一样,它工作得很好
简而言之-扩展到线程的类的类变量不支持run
方法中的对象
有关更多详细信息,请参见下面的示例代码。我已经创建了两个文件。1.包含所有对象的Objects.php和另一个Processor.php。见下文
php包含以下代码。
我终于找到了答案。但我不太确定这是否是一种正确的行为。欢迎您的建议
我所做的是创建一个类变量$this->e
,并将其分配给子进程对象$this->e=new$object
,然后运行init()
方法。init方法返回一些查询作为类成员变量,即$this->e->querys
将这些查询分配给它。我发现的解决方案是,我应该在方法中使用它作为一个单独的变量,而不是作为类变量,一旦我有了查询,我可以直接将它分配给类变量,并在执行$thread->join()
之后调用它,就像$thread->querys
一样,它工作得很好
简而言之-扩展到线程的类的类变量不支持run
方法中的对象
有关更多详细信息,请参见下面的示例代码。我已经创建了两个文件。1.包含所有对象的Objects.php和另一个Processor.php。见下文
php包含以下代码。
不从构造函数调用基类的构造函数至少在形式上是错误的。这就是说,您不会在这里启动任何线程,因为$processs\u list
是空的!?$this->querys
是否在线程结束前包含所需的值?这个值是什么类型的?$this->querys是一个包含mysql查询的数组。线程运行但不返回$this->querys数组//将mysql查询返回给线程这不是可执行代码,没有可执行代码,大家都在猜测。。。将问题简化为最简单的形式,忘记对象和当前应用程序,用尽可能少的通用代码说明问题。不从构造函数调用基类的构造函数至少在形式上是错误的。这就是说,您不会在这里启动任何线程,因为$processs\u list
是空的!?$this->querys
是否在线程结束前包含所需的值?这个值是什么类型的?$this->querys是一个包含mysql查询的数组。线程运行但不返回$this->querys数组//将mysql查询返回给线程这不是可执行代码,没有可执行代码,大家都在猜测。。。将问题简化为最简单的形式,忘记对象和当前应用程序,用尽可能少的通用代码来说明问题。如果我不使用PTHREADS\u INHERIT\u NONE,我会得到zend\u heap\u错误。可能是因为我预装了很多东西吗?我正在做的是在run方法中注销和注册我的autoloader对象,它工作正常。它自动加载Childprocess类。我使用的是PHP5.5.27和pthreads v2。如果我不使用PTHREADS\u INHERIT\u NONE,我会得到zend\u heap\u错误。可能是因为我预装了很多东西吗?我正在做的是在run方法中注销和注册我的autoloader对象,它工作正常。它自动加载Childprocess类。
<?php
class Controller {
public $queries = [];
public function init() {
}
public function set($key, $value) {
$this->{$key} = $value;
return $this;
}
function __destruct() {
global $scriptStartTime;
pr("Time taken for " . get_called_class() . " to execute = " .. executionTime($scriptStartTime, true));
}
}
class SampleObject extends Controller {
public function init() {
parent::init();
sleep(rand(0, 15));
return $this->queries[] = 'INSERT INTO my_table (something) VALUES ("' . get_called_class() . '")';
}
}
function pr($array) {
echo PHP_EOL;
print_r($array);
echo PHP_EOL;
}
function executionTime($startTime, $text = false) {
$time = @number_format(microtime(true) - $startTime, 3);
$txt = 'seconds';
if ($time > 60) {
$time = @number_format($time / 60, 3);
$txt = 'minutes';
}
if ($text) {
$time = "{$time} {$txt}";
}
return $time;
}
ini_set('display_errors', 1);
require __DIR__ . DIRECTORY_SEPARATOR . 'Objects.php';
################ Processor #################
class Processor extends \Thread {
public function __construct($process) {
$this->process = $process;
}
public function run() {
//\Core\Autoloader::reload(); // reloading all autoloaders
require __DIR__ . DIRECTORY_SEPARATOR . 'Objects.php';
$scriptStartTime = microtime(true);
# Dynamically creating objects for testing purpose.
if (!class_exists($this->process['className'])) {
eval("class " . $this->process['className'] . " extends SampleObject {}");
}
$object = (new $this->process['className']);
# Set the default values that are common across all elements
$object
# Identity of thread
->set('name', $this->process['className'])
# Options to carry the assigned values
->set('options', $this->process['options'])
# The project details
->set('project', $this->project)
;
$object->init();
$this->queries = ($object->queries);
}
}
$scriptStartTime = microtime(true);
for ($i = 0; $i < 150; $i++) {
$jobs[] = [
'className' => 'Object_' . $i,
'options' => []
];
}
$totalJobsToExecute = count($jobs);
$i = 0;
# Initalizing threads
$threads = [];
$project = [
'id' => 12345,
'title' => 'Some cool stuff'
];
foreach ($jobs AS $process) {
$i++;
$proc = $process['className'];
$threads[$proc] = new Processor($process);
// In this sample code it works without PTHREADS_INHERIT_NONE, but with my code it doesn't
if ($threads[$proc]->start(PTHREADS_INHERIT_NONE)) {
pr('Thread "' . $process['className'] . '" started');
}
}
pr("Threads | Starting time = " . executionTime($scriptStartTime, true));
$queries = [];
foreach ($threads AS $thread) {
if ($thread->join()) {
$queries[] = $thread->queries;
}
}
pr($queries);
pr('Count of threads === ' . count($threads));
pr("Threads time = " . executionTime($scriptStartTime, true));
pr("Threads | Total Threads executed = ({$i}) out of (" . $totalJobsToExecute . ")");