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

How to write and read at the same time #260

Open
xpader opened this issue Dec 20, 2024 · 1 comment
Open

How to write and read at the same time #260

xpader opened this issue Dec 20, 2024 · 1 comment

Comments

@xpader
Copy link

xpader commented Dec 20, 2024

Could it implement real streaming like one side is writing, and another side is reading?

Example code (async by amphp):

$stream = Stream::create();

async(function() use ($stream) {
    while ($stream->isReadable()) {
        echo "--read--\n";
        echo $stream->read(512)."\n";
        delay(0.1);
    }
});

for ($i=0; $i<100; $i++) {
    $content = str_repeat((string)$i, 10);
    echo "--write--\n";
    $stream->write($content);
    delay(0.1);
}

$stream->close();

Synchronize code:

$stream = Stream::create();

for ($i=0; $i<100; $i++) {
    $content = str_repeat((string)$i, 10);
    $stream->write($content);
    echo $stream->read(512)."\n";

}

$stream->close();

Can not read anything... The problem is when call write, the cursor of stream is also moved, so read can not read anything..

Currently, must be completely written, then rewind before starting to read, it's not stream.

How do I achieve true streaming while writing and reading at the same time? Imagine download a file from http request, http server is writing, and client is reading, all in streaming.

@odan
Copy link

odan commented Dec 26, 2024

As far as I know the (PHP) stream implementation is not designed for concurrent read/write operations because they share a single cursor. You may try a different approach with a producer-consumer pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@odan @xpader and others