Cara menggunakan php asynchronous processing
LoginRegister For most programs written in PHP, its sole purpose is to execute a simple process consisting of multiple tasks, where the tasks must be executed in sequence such as data processing. We always have to tolerate the stop and wait aspect of synchronous programming. The synchronous style of code execution is referred to as blocking, this implies that tasks will be executed one by one. So what if we want to run tasks without them blocking each other, that means we need to have a non-blocking process? This approach would require applying asynchronous programming approaches in PHP, here tasks will be executed without depending on of each other. A common approach to achieve a non-blocking execution in PHP is to implement queue processing. Tasks are persisted to a transport system e.g MySQL, Redis, Amazon SQS e.t.c, which is retrieved by a background worker and executed accordingly, thereby not blocking the main process in which it was created. A laravel application provides a queue mechanism that allows tasks in this case called jobs to be deferred for to a later time for processing. Another approach would be to run all defined tasks in parallel. What we get out of this approach is that a particular task is done it can return control back to the main process immediately with a promise to execute code and notify us about the result later(e.g. callback). One might see little use case for the parallel processing approach; example use case could be performing image processing and making a get request to some external service. Let’s see the difference between synchronous and asynchronous (parallel) process in PHP using a very simple use case. Synchronous code
Enter fullscreen mode Exit fullscreen mode Asynchronous code
Enter fullscreen mode Exit fullscreen mode When we execute the first code we will get the output values in this order:
Enter fullscreen mode Exit fullscreen mode Retrying the execution, we will get the output in this same sequence above … hence each multiplication operation waits to execute before the next one. Next, running the second code block, let’s see what we get.
Enter fullscreen mode Exit fullscreen mode Retrying the execution for the second time:
Enter fullscreen mode Exit fullscreen mode One process happens to produce two different results. This exactly is what we get for utilising the asynchronous approach … our little tasks can be executed in a fashion they don’t block each other. Each multiplication task executes independently, some faster than others hence the disorderliness in the output. Also, notice our async function as a then method attached to it, this method is responsible for taking back control and it accepts a callback function as its parameter which can now perform extra operations with the received output. The folks at Spatie made this nice
Enter fullscreen mode Exit fullscreen mode The package provides a neat way to interact with the tasks created, that are to be executed in parallel. The Event Listeners on the tasks are described below:
The event listeners are hooked to a task as shown below:
Enter fullscreen mode Exit fullscreen mode To learn more about the |