Download the PHP package m6web/tornado without Composer
On this page you can find all versions of the php package m6web/tornado. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download m6web/tornado
More information about m6web/tornado
Files in m6web/tornado
Informations about the package tornado
Tornado 🌪🐎
A library for asynchronous programming in Php.
Tornado is composed of several interfaces to write asynchronous programs using generators. This library provides adapters for popular asynchronous frameworks (ReactPhp, Amp) and built-in adapters to understand how to write your own.
Installation
You can install it using Composer:
You will also have to install additional dependencies related to the adapter you choose for your EventLoop
you may check our suggestions using Composer:
ℹ️ Tornado includes its own EventLoop
adapter to ease quick testing, and to show how you could write
your own EventLoop
optimized for your use case, but keep in mind that ⚠️Tornado adapters are not
yet production ready⚠️.
How to use it
You can find ready-to-use examples in examples
directory,
but here some detailed explanations about asynchronous programing, and Tornado principles.
Dealing with promises
The EventLoop
is the engine in charge of executing all asynchronous functions.
If one of those functions is waiting an asynchronous result (a Promise
)
the EventLoop
is able to pause this function and to resume an other one ready to be executed.
When you get a yield
it,
letting the EventLoop
deal internally with
Php Generators.
⚠️ Remember that the return type can NOT be array
here,
even if we expect that json_decode
will return an array
.
Since we are creating a Generator
,
the return type is by definition \Generator
.
Asynchronous functions
As soon as your function needs to wait a Promise
, it becomes by definition an asynchronous function.
To execute it, you need to use EventLoop::async
method.
The returned Promise
will be resolved with the value returned by your function.
⚠️ Keep in mind that's a bad practice to expose publicly a Generator
.
Your asynchronous functions should return a Promise
and keep its Generator
as an implementation detail, you could choose to return a Promise
in an other manner (see dedicated examples).
Running the event loop
Now, you know that you have to create a generator to wait a Promise
,
and then call Promise
…
But how can we wait the first Promise
?
Actually, there is a second way to wait a Promise
, a synchronous one:
the EventLoop::wait
method.
It means that you should use it only once, to wait synchronously the resolution of a predefined goal.
Internally, this function will run a loop to handle all events until your goal is reached
(or an error occurred, see dedicated chapter).
Like with the yield
keyword,
the Promise
,
but remember that you should use it only once during execution.
Concurrency
To reveal the true power of asynchronous programming, we have to introduce concurrency in our program.
If our goal is to send only one HTTP request and wait for it,
there is no gain to deal with an asynchronous request.
However, as soon as you have at least two goals to reach,
asynchronous functions will improve your performances thanks to concurrency.
To resolve several independent Promises
,
use Promise
that will be resolved when all others are resolved.
It's important to note that
it will be more efficient to use EventLoop::promiseAll
instead of waiting each input Promise
consecutively,
because of concurrency.
Each time that you have several promises to resolve,
ask yourself if you could wait them concurrently, especially when you deal with loops
(take a look to EventLoop::promiseForeach
function
and corresponding example).
Resolving your own promises
By design, you cannot resolve a promise by yourself, you will need a Deferred
.
It allows you to create a Promise
and to resolve (or reject) it
while not exposing these advanced controls.
Error management
A Promise
is resolved in case of success,
but it will be rejected with a Throwable
in case of error.
While waiting a EventLoop::wait
an exception may be thrown,
it's up to you to catch it or to let it propagate to the upper level.
If you throw an exception in an asynchronous function, this will reject the associated Promise
.
When using EventLoop::async
,
all exceptions thrown inside the generator will reject the returned Promise
.
In case of a background computing you may ignore this Promise
and not yield
nor wait it,
but Tornado will still catch thrown exceptions to prevent to miss them.
By design, an ignored rejected Promise
will throw its exception during its destruction.
It means that if you really want to ignore all exceptions (really?),
you have to catch and ignore them explicitly in your code.
FAQ
Is Tornado related to the Tornado Python library?
No, even if these two libraries deal with asynchronous programming, they are absolutely not related. The name Tornado has been chosen in reference to the horse ridden by Zorro.
I :heart: your logo, who did it?
The Tornado logo has been designed by Cécile Moret.
Contributing
Running unit tests:
Running examples:
Running PhpStan (static analysis):
Check code style:
Fix code style: