简体   繁体   English

如何在并行运行的脚本之间传递变量

[英]How to pass variables between scripts running in parallel

I'm running IIS on a Windows Server w/PHP 5.3. 我在带有PHP 5.3的Windows Server上运行IIS。 I have two scripts; 我有两个脚本; let's call them initiator.php and worker.php. 我们称它们为Initiator.php和worker.php。 A user calls initiator.php and in this script a variable is defined; 用户调用initiator.php,并在此脚本中定义了一个变量。 let's call it $input. 我们称之为$ input。 I would like to take this $input variable and pass it to worker.php like so: 我想把这个$ input变量传递给worker.php像这样:

$oShell = new COM('Wscript.Shell');
$oShell->Run("\"C:/Program Files (x86)/PHP/v5.3/php/worker.php -a $input",0,False);

In worker.php I have the following to pick up the $input variable passed from initiator.php. 在worker.php中,我有以下内容来拾取从Initiator.php传递的$ input变量。

$aCliOpts = getopt('a:');
$input_from_initiator = $aCliOpts['a'];

This works great. 这很好。 initiator.php's $input variable is successfully passed to worker.php which picks it up and initiator.php keeps chugging. Initiator.php的$ input变量已成功传递给worker.php,后者将其选中,initiator.php不断变化。 However, worker.php then takes it's own $input_from_initiator variable, runs through some quick code of it's own and creates a third variable called $output_from_worker. 但是,worker.php然后使用它自己的$ input_from_initiator变量,运行它自己的一些快速代码,并创建第三个变量$ output_from_worker。 It is this variable that I need initiator.php to read a little ways into it's processing. 我需要这个变量initiator.php才能对其进行处理。 This is where I'm getting hung up. 这就是我要挂断电话的地方。

I've tried passing the variable back to initiator.php from worker.php the same way it a variable as passed in the beginning and this did not work. 我尝试将变量从worker.php传递回initiator.php,就像在开始时传递一个变量一样,但这不起作用。 I've also tried to use: 我也尝试使用:

header('Location: initiator.php?var=value') 

using HTTP GET params but to no avail. 使用HTTP GET参数,但无济于事。

My last resort is for worker.php to write this variable's value to disk then have initiator.php read from disk. 我的最后一招是worker.php将此变量的值写入磁盘,然后从磁盘读取initiator.php。 I hate to do this due to the latent disk I/O. 由于潜在的磁盘I / O,我不愿意这样做。 Speed is very important to this script. 速度对于此脚本非常重要。

Is there a way two PHP processes can pass variables between each other in memory? 有两个方法可以在内存中彼此之间传递变量吗?

Have a look at file_get_contents() http://php.net/file_get_contents , which you can pass a URL to. 看看file_get_contents() http://php.net/file_get_contents ,您可以通过一个URL。 So you could use the Query String like: 所以,你可以使用查询字符串,如:

 $var = file_get_contents('http://site.tld/worker.php?input='.$input);

And in worker.php , simply echo your result. 而在worker.php ,只是附和你的结果。

It's a shame you're running on Windows, because the sys5 extension on *nix is marvelous! 您在Windows上运行很可惜,因为* nix上的sys5扩展很棒!

You can always use files or a database etc for communication. 您可以始终使用文件或数据库等进行通信。

Thanks for the help although I ended up doing something a little different than my original question. 感谢您的帮助,尽管我最终做的事情与原始问题有所不同。 I was trying to run different cURL requests. 我试图运行不同的cURL请求。 curl_multi_exec() ended up working great for me. curl_multi_exec()最终对我来说很棒。

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

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