简体   繁体   English

如何将 output 打印到 php 中的两个不同终端?

[英]How to print output to two different terminals in php?

I am running two for loops in parallel using amphp/parallel-functions.我正在使用 amphp/parallel-functions并行运行两个for loops I am wondering how can I see the live output printing parallelly?我想知道如何看到实时 output 并行打印? I am thinking of printing to two different terminals.我正在考虑打印到两个不同的终端。 Can it be possible?有可能吗? If no how can I track the output if it is running parallelly or sequentially?如果没有,我如何跟踪 output 如果它是并行运行还是顺序运行?

<?php
require "vendor/autoload.php";

use Amp\Promise;
use Amp\ParallelFunctions;

$promises[1] = ParallelFunctions\parallel(function (){

    for ($i=0; $i<50; $i++){
        echo 'Promise-1 : '. $i . PHP_EOL;
    }

})();

$promises[2] = ParallelFunctions\parallel(function (){

    for ($i=0; $i<50; $i++){
        echo 'Promise-2 : '. $i .PHP_EOL;
    }

})();

Promise\wait(Promise\all($promises));

Current Output seems to be sequential当前 Output 似乎是顺序的

Promise-1 : 0
Promise-1 : 1
Promise-1 : 2
Promise-1 : 3
Promise-1 : 4
Promise-2 : 0
Promise-2 : 1
Promise-2 : 2
Promise-2 : 3
Promise-2 : 4

Note I am using a mac and this is purely to test on my local.注意我使用的是mac,这纯粹是为了在我的本地测试。

Disclaimer : Printing to a terminal is going to be inherently very dependent on the OS and not really a very stable system: What do you do when the user closes the terminal?免责声明:打印到终端本质上将非常依赖于操作系统,而不是一个非常稳定的系统:当用户关闭终端时你会做什么? What happens if you have something else already running on the first terminal?如果您在第一个终端上已经运行了其他东西会发生什么?

With that out of the way here is how you can do it:有了这个,你可以这样做:

On most versions of linux you can write to the virtual files found at /dev/pts/ (for example /dev/pts/1) to output on any open terminal.在大多数版本的 linux 上,您可以在任何打开的终端上将 /dev/pts/(例如 /dev/pts/1)中的虚拟文件写入 output。 You can try it out by opening two terminal instances and typing the following in the second one:您可以通过打开两个终端实例并在第二个终端实例中键入以下内容来尝试一下:

echo "testing" > /dev/pts/0

This should print testing on the first terminal.这应该在第一个终端上打印测试。 However it won't work on mac, since there is no /dev/pts folder, however you can instead type the following in the second terminal:但是它在 mac 上不起作用,因为没有 /dev/pts 文件夹,但是您可以在第二个终端中键入以下内容:

echo "testing" > /dev/ttys000

or in php:或在 php 中:

$fp = fopen('dev/ttys000');
fwrite($fp, "testing\n");
fclose($fp);

If it is possible at all on Windows, it's almost certainly much more complicated than writing to a file.如果在 Windows 上完全可行,那几乎肯定比写入文件要复杂得多。

In general I recommend writing the output to separate log files in this sort of situation instead of the terminal一般来说,我建议在这种情况下编写 output 来分隔日志文件,而不是终端

amphp/parallel uses child processes to run these in parallel. amphp/parallel使用子进程来并行运行这些。 STDOUT and STDERR of the child processes are piped into the parent's STDOUT and STDERR respectively.子进程的 STDOUT 和 STDERR 分别通过管道传输到父进程的 STDOUT 和 STDERR。 As streams are read and written in chunks, the output order does likely not represent the actual execution order.由于流是按块读取和写入的,因此 output 顺序可能并不代表实际的执行顺序。

If you put sleep calls into the for loops, you'll likely be able to observe the interleaved execution order.如果您将sleep调用放入for循环中,您可能能够观察到交错的执行顺序。

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

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