Skip to content

Commit

Permalink
Updated example
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamsxin committed Dec 4, 2020
1 parent d8d0811 commit 2ed1a8b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
55 changes: 55 additions & 0 deletions examples/async/Console2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

error_reporting(-1);
ini_set('display_errors', (DIRECTORY_SEPARATOR == '\\') ? '0' : '1');

$stdin = \Phalcon\Async\Stream\ReadablePipe::getStdin();

$builder = \Phalcon\Async\Process\ProcessBuilder::shell(true);
$builder = $builder->withStdoutPipe();
$builder = $builder->withStderrPipe();
//$builder = $builder->withCwd(__DIR__);
//$builder = $builder->withStdoutInherited();
//$builder = $builder->withStderrInherited();
$process = $builder->start();

$shellstdin = $process->getStdin();

$channel = new Phalcon\Async\Channel;

$reader = function (Phalcon\Async\Process\ReadablePipe $pipe) use ($channel) {
try {
try {
while (($out = $pipe->read()) !== NULL) {
echo $out;
if ($out[-1] == chr(0x0a)) { //enter
$channel->send('ok');
}
}
} catch (\Exception $e) {
}
} finally {
$pipe->close();
}
};

$defer = new \Phalcon\Async\Deferred();

Phalcon\Async\Task::async($reader, $process->getStdout());
Phalcon\Async\Task::async($reader, $process->getStderr());

while (1) {
echo 'php>';
if (null !== ($chunk = $stdin->read()) && $process->isRunning()) {
$shellstdin->write($chunk);
$iter = $channel->getIterator();
$iter->current();

echo PHP_EOL;
} else {
echo Phalcon\Cli\Color::info('shell exit').PHP_EOL;
}
}
echo 'close';

var_dump($process->join());
6 changes: 4 additions & 2 deletions ext/async/process/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,15 @@ static PHP_METHOD(ReadablePipe, read)
async_stream_read_req read;

zval *hint;
zend_long timeout = 0;
size_t len;

hint = NULL;

ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 1)
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(hint)
Z_PARAM_LONG(timeout)
ZEND_PARSE_PARAMETERS_END();

pipe = (async_readable_process_pipe *) Z_OBJ_P(getThis());
Expand All @@ -819,7 +821,7 @@ static PHP_METHOD(ReadablePipe, read)
read.in.len = len;
read.in.buffer = NULL;
read.in.handle = NULL;
read.in.timeout = 0;
read.in.timeout = timeout;
read.in.flags = 0;

if (EXPECTED(SUCCESS == async_stream_read(pipe->state->stream, &read))) {
Expand Down

0 comments on commit 2ed1a8b

Please sign in to comment.