8000 Add a word of warning regarding fopen() and family · reactphp/stream@b326612 · GitHub
[go: up one dir, main page]

Skip to content

Commit b326612

Browse files
committed
Add a word of warning regarding fopen() and family
1 parent cb670a7 commit b326612

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,14 @@ creating a stream instance.
780780
However, if you are writing a lower-level component or want to create a stream
781781
instance from a stream resource, then the following chapter is for you.
782782

783+
> Note that the following examples use `fopen()` and `stream_socket_client()`
784+
for illustration purposes only.
785+
These functions SHOULD NOT be used in a truly async program because each call
786+
may take several seconds to complete and would block the EventLoop otherwise.
787+
Additionally, the `fopen()` call will return a file handle on some platforms
788+
which may or may not be supported by all EventLoop implementations.
789+
As an alternative, you may want to use higher-level libraries listed above.
790+
783791
### ReadableResourceStream
784792

785793
The `ReadableResourceStream` is a concrete implementation of the
@@ -1064,17 +1072,27 @@ $through->write(2);
10641072
```
10651073

10661074
## Usage
1075+
1076+
The following example can be used to pipe the contents of a source file into
1077+
a destination file without having to ever read the whole file into memory:
1078+
10671079
```php
1068-
$loop = React\EventLoop\Factory::create();
1080+
$loop = new React\EventLoop\StreamSelectLoop::create();
10691081

1070-
$source = new React\Stream\ReadableResourceStream(fopen('omg.txt', 'r'), $loop);
1071-
$dest = new React\Stream\WritableResourceStream(fopen('wtf.txt', 'w'), $loop);
1082+
$source = new React\Stream\ReadableResourceStream(fopen('source.txt', 'r'), $loop);
1083+
$dest = new React\Stream\WritableResourceStream(fopen('destination.txt', 'w'), $loop);
10721084

1073-
$source->pipe($dest);
1085+
$source->pipe($dest);
10741086

1075-
$loop->run();
1087+
$loop->run();
10761088
```
10771089

1090+
> Note that this example uses `fopen()` for illustration purposes only.
1091+
This should not be used in a truly async program because the filesystem is
1092+
inherently blocking and each call could potentially take several seconds.
1093+
See also [creating streams](#creating-streams) for more sophisticated
1094+
examples.
1095+
10781096
## Install
10791097

10801098
The recommended way to install this library is [through Composer](http://getcomposer.org).

0 commit comments

Comments
 (0)
0