8000 Restructure and improve README by jsor · Pull Request #90 · reactphp/event-loop · GitHub
[go: up one dir, main page]

Skip to content

Restructure and improve README # 8000 90

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

Merged
merged 2 commits into from
Mar 27, 2017
Merged
Changes from 1 commit
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
Next Next commit
Restructure and improve README
* Separate into sections and add a TOC at the beginning
* Add new sections for Install, Tests and License
* Add paragraph to Usage emphasizing on recommended program structuring using a single loop instance
  • Loading branch information
jsor committed Mar 27, 2017
commit 03f6d8f0edcb119c725c40607a58844952e33ac5
131 changes: 100 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,16 @@

Event loop abstraction layer that libraries can use for evented I/O.

In order for async based libraries to be interoperable, they need to use the
same event loop. This component provides a common `LoopInterface` that any
library can target. This allows them to be used in the same loop, with one
single `run` call that is controlled by the user.

In addition to the interface there are some implementations provided:

* `StreamSelectLoop`: This is the only implementation which works out of the
box with PHP. It does a simple `select` system call. It's not the most
performant of loops, but still does the job quite well.

* `LibEventLoop`: This uses the `libevent` pecl extension. `libevent` itself
supports a number of system-specific backends (epoll, kqueue).

* `LibEvLoop`: This uses the `libev` pecl extension
([github](https://github.com/m4rw3r/php-libev)). It supports the same
backends as libevent.
**Table of Contents**

* `ExtEventLoop`: This uses the `event` pecl extension. It supports the same
backends as libevent.
* [Quickstart example](#quickstart-example)
* [Usage](#usage)
* [Loop implementations](#loop-implementations)
* [Install](#install)
* [Tests](#tests)
* [License](#license)

All of the loops support these features:

* File descriptor polling
* One-off timers
* Periodic timers
* Deferred execution of callbacks

## Usage
## Quickstart example

Here is an async HTTP server built with just the event loop.

Expand All @@ -42,6 +23,7 @@ $loop = React\EventLoop\Factory::create();

$server = stream_socket_server('tcp://127.0.0.1:8080');
stream_set_blocking($server, 0);

$loop->addReadStream($server, function ($server) use ($loop) {
$conn = stream_socket_accept($server);
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
Expand All @@ -65,7 +47,94 @@ $loop->addPeriodicTimer(5, function () {
$loop->run();
```

**Note:** The factory is just for convenience. It tries to pick the best
available implementation. Libraries `SHOULD` allow the user to inject an
instance of the loop. They `MAY` use the factory when the user did not supply
a loop.
## Usage

In order for async based libraries to be interoperable, they need to use the
same event loop. This component provides a common `LoopInterface` that any
library can target. This allows them to be used in the same loop, with one
single `run()` call that is controlled by the user.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this paragraph stay at the top, right below the tag line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i was unsure either and moved the paragraph up and down several times while working on the patch 😃

In the end i decided to put it under Usage because i thought it fits there and might be easily overlooked at the beginning. My guess is, that users skip the "promotion" parts at just jump straight to the usage section.

But i'd be fine with both locations. @WyriHaximus, @cboden?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial paragraph should try to sell this idea to newcomers and explain the WHY. This is also what will be shown as an "abstract" in many applications, e.g. the first paragraph will be shown in mobile view, while the rest will be hidden by default.

IMHO the why is the most important part of the documentation, but we may as well duplicate this below if this makes sense. Otherwise, we may also fine-tune this later on 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the paragraph back to the top and replaced it here with a short introduction.


```php
// [1]
$loop = React\EventLoop\Factory::create();

// [2]
$loop->addPeriodicTimer(1, function () {
echo "Tick\n";
});

$stream = new React\Stream\ReadableResourceStream(
fopen('file.txt', 'r'),
$loop
);

// [3]
$loop->run();
```

1. The loop instance is created at the beginning of the program. A convenience
factory `React\EventLoop\Factory::create()` is provided by this library which
picks the best available [loop implementation](#loop-implementations).
2. The loop instance is used directly or passed to library and application code.
In this example, a periodic timer is registered with the event loop which
simply outputs `Tick` every second and a
[readable stream](https://github.com/reactphp/stream#readableresourcestream)
is created by using ReactPHP's
[stream component](https://github.com/reactphp/stream) for demonstration
purposes.
3. The loop is run with a single `$loop->run()` call at the end of the program.

## Loop implementations

In addition to the interface there are the following implementations provided:

* `StreamSelectLoop`: This is the only implementation which works out of the
box with PHP. It does a simple `select` system call. It's not the most
performant of loops, but still does the job quite well.

* `LibEventLoop`: This uses the `libevent` pecl extension. `libevent` itself
supports a number of system-specific backends (epoll, kqueue).

* `LibEvLoop`: This uses the `libev` pecl extension
([github](https://github.com/m4rw3r/php-libev)). It supports the same
backends as libevent.

* `ExtEventLoop`: This uses the `event` pecl extension. It supports the same
backends as libevent.

All of the loops support these features:

* File descriptor polling
* One-off timers
* Periodic timers
* Deferred execution of callbacks

## Install

The recommended way to install this library is [through Composer](http://getcomposer.org).
[New to Composer?](http://getcomposer.org/doc/00-intro.md)

This will install the latest supported version:

```bash
$ composer require react/event-loop
```

## Tests

To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](http://getcomposer.org):

```bash
$ composer install
```

To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
```

## License

MIT, see [LICENSE file](LICENSE).
0