Welcome to asynchronous multitasking PHP

Amp is a non-blocking concurrency framework for PHP applications.

What is asynchronous non-blocking I/O?

Usually, at least in the PHP world, everything happens synchronously in your code. When you make a database query, you call a function. That function sends the query to the server, then it waits (or "blocks") until it gets a response and returns the retrieved value.

We can do better than that. With non-blocking I/O we can perform other tasks while waiting for the database server to return a result. We could fire off a second query in the meantime or perform any other task.

You might respond "What's so bad about waiting? The kernel will just schedule other tasks during that waiting time?" While that is true, and it's how most applications work today, it's not the end of the story.

Let's make a comparison with cooking a soup. With blocking I/O, you first have to peel the potatos and other vegetables and then slice them (CPU work). Once you're finished, you can turn on the stove. Now you're waiting for the water in the pot to become hot. It's hot now, so you can put all vegetables into the pot and wait for it to be done.

With non-blocking I/O, you could have turned on your stove directly. You could delegate the actual hard work of slicing the vegetables to workers (your children). While being in idle waiting for the stove and the workers, you could stream a video from YouTube and watch it. Once everything is ready, you can put it into the pot. While you're waiting for it to be done, you can play a game with your children.

But how does it work in reality? All I/O is managed by the kernel. We just write to the kernel's buffer, we don't wait until the data is actually sent to the other side. In the mean time, we can do other things. Once we have an answer, the kernel will notify us that there's a new readable event.

This waiting time isn't limited to databases, it's more and more important as you add I/O to your application, e.g. by using microservices. Non-blocking I/O makes more efficient use of your processes, because you know, processes cost memory and need to be scheduled.

Hello World

Let's see how it would actually look like in code. We don't use I/O in this hello world, instead we use another type of event - timers. Just click "Run" to see what happens.

We emulate this simple example in javascript, but if you want to see it live on your local machine with PHP, just clone our hello-world repository and run php timer.php.


function tick() {
    echo "tick\n";

echo "before run()\n";

Amp\run(function() {
    Amp\repeat("tick", $msInterval = 1000);
    Amp\once("Amp\stop", $msDelay = 5000);

echo "after run()\n";




We already have a good set of libraries and packages you can use. Not all of them have a documentation yet, you're welcome to help. You can install all our libraries using Composer. Just have a look at the blue top menu listing the packages. It's indicated whether they have documentation here already or not through the icon on the right.

composer require amphp/amp
Continue with Installing Amp.