16

I have created a process using proc-open but under windows the stream-select does not work. What I am trying to achieve is to read from both stdout and stderr, whilst in addition writing to stdin and ensure that the output can be matched up with the inputs. Is there a workaround for windows to overcome this deficiency?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • 2
    I think the only thing you can do is to set your pipes in non-blocking mode and check them manually, unfortunately. – netcoder Mar 28 '12 at 14:17
  • 1
    @netcoder - I have tried this. `stream_set_blocking` returns false :-( – Ed Heal Mar 28 '12 at 14:21
  • stderr and stdout cannot be set to non blocking. What are you trying to achieve exactly? Have some code? select may not be necessary here. – Pierre Apr 01 '12 at 15:49
  • Can you distinguish messages printed to `stderr` from messages printed to `stdout` (by using some sort of regexp for example)? – galymzhan Apr 06 '12 at 16:12
  • @galymzhan - The two streams are diffenent in the fact that they come from two different file descriptors. Also, sometimes there is nothing to be read. – Ed Heal Apr 06 '12 at 17:06
  • @EdHeal I wanted to suggest to redirect `stderr` to `stdout`, so you'll have to worry about `stdout` only. Not sure how to implement non-blocking access, though – galymzhan Apr 06 '12 at 17:35
  • @galymzhan - but either way the output might not be av available. Sort of want the PHP to act as expect. – Ed Heal Apr 06 '12 at 18:12

2 Answers2

1

You are not very detailed on what is not working for you with stream-select on Windows. However, this is a working example on how to use stream-select. I just ran this successfully with PHP 5.4 on Windows XP.

Edit: Uhhmmm.. Seems like it was non-working after all... Sry, testing some more here.. :)

Edit again:

So, I did some more experimenting on this, but unsuccessfully.

Maybe you should just let the processes speak TCP/UDP/IP to each other?

Another way forward (if you still want to use stdout/stdin/stderr in your process) might be to use proc_open with file handlers, so your process is writing to files, and then use something similar to unix' inotify, loading this stuff with the PHP DOTNET class: Is there anything like inotify on Windows? to detect changes to the files...? Just an idea...

Or maybe change to a unix-like os? ;) Ok, that's it for me tonight. Good night

Community
  • 1
  • 1
Alfred Godoy
  • 1,045
  • 9
  • 19
  • 1
    `stream_select` in your example seems useless. It just reads from `stdout` and `stderr` pipes without actually looking into what is returned in `$read_streams`. This could block if child process is busy (or child wrote into `stderr` when you're waiting data on `stdout`) – galymzhan Apr 06 '12 at 14:09
  • 1
    @AlfredGodoy - The problem is that windows `select` does not work. But unix/linux it seems to work. Sorry for the effort for code that does not work. – Ed Heal Apr 06 '12 at 16:30
  • @AlfredGodoy - You get the bounty for the effort. Should not go to waste. – Ed Heal Apr 06 '12 at 17:13
  • Thnx :) Added some more ideas on this in the answer. – Alfred Godoy Apr 06 '12 at 22:13
  • @AlfredGodoy - I will put you on my list of kind people. – Ed Heal Apr 07 '12 at 07:14
  • @AlfredGodoy -Ask me a question and I will try to give you an answer – Ed Heal Apr 07 '12 at 10:23
1

adding bypass_shell worked for me

$proc=proc_open($cmd,
        array(
            0=>array('pipe', 'r'), //stdin
            1=>array('pipe', 'w'), //stdout
            2=>array('pipe', 'w')  //stderr
            ),
        $pipes,
        $dir,
        null,
        array('bypass_shell'=>true)
    );
Steve Lloyd
  • 773
  • 2
  • 12
  • 33