Skip to content

Commit c33d7f1

Browse files
committed
Add a word of warning regarding fopen() and family
1 parent 0ef4311 commit c33d7f1

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
@@ -781,6 +781,14 @@ creating a stream instance.
781781
However, if you are writing a lower-level component or want to create a stream
782782
instance from a stream resource, then the following chapter is for you.
783783

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

786794
The `ReadableResourceStream` is a concrete implementation of the
@@ -1065,17 +1073,27 @@ $through->write(2);
10651073
```
10661074

10671075
## Usage
1076+
1077+
The following example can be used to pipe the contents of a source file into
1078+
a destination file without having to ever read the whole file into memory:
1079+
10681080
```php
1069-
$loop = React\EventLoop\Factory::create();
1081+
$loop = new React\EventLoop\StreamSelectLoop::create();
10701082

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

1074-
$source->pipe($dest);
1086+
$source->pipe($dest);
10751087

1076-
$loop->run();
1088+
$loop->run();
10771089
```
10781090

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

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

0 commit comments

Comments
 (0)