Goal: run a PHP file in cmd, script loops x times and on every iteration checks to see if user has entered any input (stream_select() with STDIN) and if so - pauses the loop until the user hits enter, then prints out the input and continues with iteration.
Problem: Script runs perfectly as long as cmd.exe window is in focus - when I click on another window the script pauses at stream_select
and doesn't continue until I but the cmd window back in focus and send it some input (a simple enter key press would do the trick). No errors.
Question: why does losing focus on cmd affect stream_select and block the loop? ...and is there a workaround? (e.g. is it possible to check if the current cmd window is in focus?)
Code example, used cmd php script.php
in working directory.
<?php
$loopCount = 20;
while ($loopCount) {
$start = microtime(true);
echo 'check on "' . $loopCount . '"' . PHP_EOL;
$stream = fopen('php://stdin', 'r');
$stream_array = array($stream);
$write = array();
$except = array();
if (stream_select($stream_array, $write, $except, 1, 0)) {
$input = trim(fgets($stream));
if ($input) {
echo 'input was "' . $input . '"' . PHP_EOL;
}
}
fclose($stream);
echo $loopCount . ' in ' . (microtime(true) - $start) . PHP_EOL;
$loopCount--;
}
Things I have tried with no luck:
- moving
fopen
andfclose
outside the loop ignore_user_abort(1);
stream_set_blocking($stream, 0);
null
,0
and higher values for bothtv_sec
andtv_usec
params ofstream_select()
- checking for connection_aborted() and connection_status()
Environment: Windows 7, XAMPP for windows, PHP 5.4.19 (cli), Zend Engine v2.4.0