Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(io): fix readAll() blocks #270

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/io/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
use function microtime;
use function round;
use function strlen;
use const PHP_OS_FAMILY;

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function (): int {
if (PHP_OS_FAMILY === 'Windows') {
IO\output_handle()->writeAll('This example requires does not support Windows.');

return 0;
}

$args = getopt('i:o:t:');
$input_file = $args['i'] ?? '/dev/zero';
Expand Down
9 changes: 6 additions & 3 deletions examples/shell/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
$start = time();

Async\concurrent([
static fn() => Shell\execute('sleep', ['2']),
static fn() => Shell\execute('sleep', ['2']),
static fn() => Shell\execute('sleep', ['1']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']),
]);

$duration = time() - $start;
Expand Down
7 changes: 7 additions & 0 deletions examples/unix/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
use Psl\IO;
use Psl\Str;
use Psl\Unix;
use const PHP_OS_FAMILY;

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function (): int {
if (PHP_OS_FAMILY === 'Windows') {
IO\output_handle()->writeAll('This example requires does not support Windows.');

return 0;
}

$file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock";

$output = IO\output_handle();
Expand Down
8 changes: 5 additions & 3 deletions src/Psl/IO/ReadHandleConvenienceMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ trait ReadHandleConvenienceMethodsTrait
*/
public function readAll(?int $max_bytes = null, ?float $timeout = null): string
{
$to_read = $max_bytes ?? Internal\ResourceHandle::DEFAULT_READ_BUFFER_SIZE;
$to_read = $max_bytes;

/** @var Psl\Ref<string> $data */
$data = new Psl\Ref('');
Expand All @@ -51,8 +51,10 @@ static function () use ($data): void {
/** @psalm-suppress MissingThrowsDocblock */
$chunk = $this->read($chunk_size, $timer->getRemaining());
$data->value .= $chunk;
$to_read -= strlen($chunk);
} while ($to_read > 0 && $chunk !== '');
if ($to_read !== null) {
$to_read -= strlen($chunk);
}
} while (($to_read === null || $to_read > 0) && $chunk !== '');

return $data->value;
}
Expand Down