简体   繁体   English

PHP 并行如何将变量传递给线程?

[英]PHP parallel how to pass variable to thread?

i use the Parallel PHP library, but i don't know how to pass variable of my script to a parallel thread.我使用并行 PHP 库,但我不知道如何将我的脚本变量传递给并行线程。 I view in the documentation that i must pass a array argument in the "run" function but i don't know how to access to the argument value.我在文档中查看我必须在“运行”function 中传递一个数组参数,但我不知道如何访问参数值。

My script:我的脚本:

<?php
$runtime = new \parallel\Runtime();


$future1 = $runtime->run(function(){

echo argv

},array(22,'hi')); // here the argv but how to access in this in $future1 ?
?>

The documentation link about parallel run function: https://www.php.net/manual/en/parallel-runtime.run.php并行运行function的文档链接: https://www.php.net/manual/en/parallel-runtime.run.php

Thanks.谢谢。

I solved my problem.我解决了我的问题。 I fell on it really by a stroke of luck.我真的是碰巧碰上了它。 Here is the solution: it was also enough to specify in the closure in the same order as the array the name associated with the values of the array.这是解决方案:在闭包中以与数组相同的顺序指定与数组值关联的名称也足够了。 For example, here is an example:例如,这里有一个例子:

<?php
$runtime = new \parallel\Runtime();

$myvar = 'hello';
$var2 = 'Guys';
$future1 = $runtime->run(function($first,$second){

echo $first.PHP_EOL; // hello
echo $second; //Guys

},array($myvar,$var2));

/*Result: 
hello
Guys
*/
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM