6

PHP in the Async-land

 1 year ago
source link: https://devm.io/php/php-in-the-async-land
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The world is asynchronous

PHP in the Async-land


When working on this article, I was thinking about real-life examples to explain asynchronous processing from scratch. In the end, I understood that there are too many examples and I can’t pick the most relevant one. Seriously, you barely can imagine a process in nature that follows a sequence or an organised queue. And asynchronous doesn’t mean chaotic.

For example, a beehive is a single unit, living as a well-managed system, despite every bee acting independently, busy with its own task. A human body is another example, where everybody’s organs are acting with its own goal and process. However, everything is perfectly coordinated with repeating heartbeats, ticking constantly, and pushing the blood through all the system members.

In fact, it is much harder to imagine something really synchronous in nature. Can you imagine the bees, waiting in queue for their turn to enter the hive, to put some honey into it? Ridiculous!

‘Why,’ said the Dodo, ‘the best way to explain it is to do it.’ (And, as you might like to try the thing yourself, some winter day, I will tell you how the Dodo managed it.) – Lewis Carroll, Alice in Wonderland

Down the hole

Diving deeper into the topic, PHP is a general-purpose scripting language, especially suited for web development. In contrast to, for example, Java, PHP has no built-in server. Well, actually, it has one, but it’s an experimental feature, not intended for production usage. If you want to execute a PHP application, you either execute it as a single script run via command-line interface (CLI) or run it as an ongoing server process, for example, using FPM (Fast Process Manager), which can manage child PHP processes and ensure that it will always be available (at least one available) to run your code.

In the case of CLI, everything is very easy – a single process is being executed to process a single request. PHP compiles code dynamically, on-demand. This means PHP will make a start-up, initialise the engine, and included modules. After it’s done, PHP will compile and execute your code. At the end of processing, it will shut down.

With FPM all will be the same, but the system will try to be prepared for the request coming from the server. FPM will make sure that the running PHP process already exists in the pool, ready to receive requests. Depending on the process management type – static, dynamic, or on-demand, it will define how many child processes have to be executed to process incoming requests from the webserver. However, each of these processes is executed to process a single request at once. So every other request is landed in the queue and is waiting for its turn at a mad tea party.

And it doesn’t mean that the working process is using any resources of the CPU at every moment of waiting time. It might be that some external request was sent, and the process is just waiting for the response, to proceed further with a running request. It would be cool if we could use the resources smarter and go on with another task during the waiting time, and continue the current one, when waiting time releases.

Have you guessed the riddle yet?

Don’t give it up! Let me guess, some of you were already facing similar situations, working with data import or export with an external system, and we’re thinking about how it would be to start a new PHP process or thread directly from your current run. There are some existing extensions available with PHP, i.e., pthreads or parallel – both, actually, require a build of PHP with Zend Thread Safety module enabled (https://www.php.net/manual/en/intro.pthreads.php , https://www.php.net/manual/en/parallel.setup.php). I might write another article later on, digging deeper into multithreading. However, now we are looking for a clear, easy, and controlled way to run the code in a non-blocking manner, without actually needing to use multithreading, or think about multiprocessing.

And keep in mind, better performance and better end-user satisfaction are the main goals here, as we want to use our resources more effectively. Sometimes, we need to run PHP code asynchronously, even if it was not originally supposed to be launched in such a way.

First of all, we need to understand, what exactly does asynchronous processing mean? It’s a kind of design for computing tasks, executing them without any dependencies between each other. Each task is different and the sequence of execution doesn’t guarantee the same order of result delivery (Picture 1).

Fig. 1

Picture 1

When we are running the webserver, we have the same behavior of serving web pages to the browser users. To make this happen like in the picture, we’d need to have at least four PHP processes executed by FPM. If we look in frames of a single PHP process, each task is queued and executed one after another (Picture 2).

Fig. 2

Picture 2

But is it possible to have one single PHP process executing multiple tasks in parallel, like in Picture 1? If Task A makes some external request, for example, to the data store and waits some time for the result, we can leave the reference open and execute Task B during the waiting time. When task B has the same external request, a program can check if the result of the request in task A is already ready and return it to the user, then continue with task B.

The best way to explain it is to program it

Implementation in PHP might be tricky, but possible. For example, a request to the data store might be implemented not as a real database request, but placing the task into the queue, checking...


Recommend

  • 46
    • www.tuicool.com 6 years ago
    • Cache

    Adventures in Navigation Land – Part 1

    In the recent series on Maintainable Architecture , the final task that we covered was separating out the Navigation logic by means of t...

  • 55
    • www.tuicool.com 6 years ago
    • Cache

    Adventures in Navigation Land – Part 2

    In the recent series on Maintainable Architecture , the final task that we covered was separating out the Navigation logic by means of t...

  • 31

    Many of the most beautiful areas in the US are owned by the government, to preserve them and allow access for everyone to enjoy them. And most US schools are traditionally closed during the summer, which provides families...

  • 52

    Who still uses eval() ? Eval() has a bad reputation for security. It also signals code that should be rewritten with PHP native features.

  • 36
    • www.tuicool.com 5 years ago
    • Cache

    Changes in Flathub land

    The last month or so we’ve been working in the background on a major update to the Flathub infrastructure. This has been available fo...

  • 15
    • play.bot.land 4 years ago
    • Cache

    Bot Land

    Bot Land was taken completely offline as of October 22nd, 2020. It was a fun run, everybody. Discord

  • 29
    • 微信 mp.weixin.qq.com 3 years ago
    • Cache

    浅谈Living Off the Land Binaries

    这是  酒仙桥六号部队  的第  92   篇文章。 全文共计2608个字,预计阅读时长9分钟 。...

  • 8

    Fibers in PHP: A new opportunity for async PHP? It looks like PHP will get fibers soon with PHP 8.1! That’s awesome! Or is it? What are fibers? I think...

  • 0
    • ndench.github.io 2 years ago
    • Cache

    What’s new in PHP land? - Q3 2020

    What's new in PHP land? - Q3 2020 Nov 16, 2020 I co-organise the BrisPHP Meetup and at the start of...

  • 2
    • code.tutsplus.com 2 years ago
    • Cache

    How to Use async in PHP

    How to Use async in PHP There are two ways of executing the tasks in any program that you write. The tasks will either be executed one after the other sequentially or they will be executed in parallel without waiting for a previous...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK