You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive. Pur potendo contare su progetti come ReactPHP e Guzzle, fino ad ora PHP si è evoluto nativamente come un linguaggio basato su codice sincrono, questo significa ad esempio che l'esecuzione di una funzione viene interrotta fino alla restituzione di un risultato. Tale dinamica può essere spiegata a larghe linee tramite il problema chiamato "Di che colore è la tua funzione?". Se infatti assumessimo che ogni funzione debba essere associata ad un colore e che il modo in cui avviene la chiamata ad una funzione dipenda da questo dato, avremmo l'unica possibilità di chiamare una funzione di un determinato colore da un'altra funzione dello stesso colore. Funzioni sincrone e asincroneIl meccanismo descritto non è forse intuitivo ma fondamentalmente ciò avviene perché le funzioni sincrone restituiscono valori mentre quelle asincrone invocano callback, nello stesso modo le funzioni sincrone possono offrire il loro risultato sotto forma di valore di ritorno mentre il risultato delle asincrone dipende sempre dal callback. Rispetto ad una sincrona una funzione asincrona modifica inoltre il modo in cui una funzione deve essere chiamata. Ora abbiamo che le funzioni sincrone non possono chiamare le asincrone mentre può avvenire il contrario, ma per la chiamata ad una funzione asincrona è necessario che l'intero stack di chiamata sia asincrono. Per questa ragione, utilizzando un linguaggio che la supporta, se una funzione restituisce una promise in uno stack di chiamata il risultato non potrà essere noto fino alla risoluzione della promise stessa, per far questo però l'intero stack deve poter restituire una promise. Fiber e funzioni asincronePer rimediare al limite dovuto alla mancanza di codice asincrono, PHP 8.1 introduce il concetto di Fiber a cui fanno riferimento una classe omonima, una di riflessione ( Le Fiber sono state implementate per rimuovere la differenza tra codice sincrono e asincrono consentendo di interrompere le funzioni senza che ciò coinvolga il call stack nel suo insieme. In pratica le Fiber mettono in pausa lo stack di esecuzione, quindi per le chiamate dirette delle funzioni non si deve modificare il modo in cui esse sono invocate. Di seguito l'esempio riportato nella RFC di PHP 8.1.
What are PHP Fibers, Swoole Coroutines, non-blocking IO and async PHP? What is the difference with PHP Fibers RFC and Swoole Coroutines? In the PHP world, there are a lot of options for asynchronous programming but this wasn't always true for PHP. Developers have mostly used PHP in a blocking, synchronous stateless manner. Over the years since PHP 7 was released where we saw huge performance improvements to the core and many language additions and now PHP 8 with the new JIT (Just in Time) engine, all this has tackled a lot of the performance and programming issues PHP once had. Most other popular web frameworks from other stacks have taken advantage of the event loop model, such as Node.js or Python's Tornado, enabling them to achieve higher throughput and concurrency, something which is important when scaling an application. But now, PHP has many options for taking advantage of the event loop model and asynchronous programming, something that once wasn't a thing in the PHP world. Recently, a new PHP RFC has been passed to enable more support for asynchronous programming, the Fibers RFC, targeting PHP 8.1, which is very similar to the way Swoole Coroutines work, with that in mind it is good to go through the differences and understand how the PHP Fibers RFC compares against Swoole Coroutines, which can be classed as Fibers as well. The proposed Fiber RFC for PHP is a way of implementing coroutines (also known as green-threads), where code can be written synchronously but executed concurrently, a Fiber (or coroutine) is a lightweight thread of execution that achieves cooperative multitasking based on I/O. Cheap and lightweight because everything is kept within the same address space/thread, does not require switching between processes which have a great impact. The Fibers RFC has been introduced to provide a basic starting point for native asynchronous programming in PHP and only solves a part of what is required to truly take advantage of Fibers/coroutines. Swoole implements coroutines based on I/O switching and can be simply understood as a user-land thread, just like how Fibers work. Swoole coroutines are cheap to execute and lightweight when switching between different fibers/coroutines. Within Swoole, coroutines are managed by the coroutine scheduler and can automatically switch context based on I/O operations, for example, when waiting for a database query to return, another coroutine can be executed. Coroutines in Swoole are very similar to how goroutines work in Go-lang and does not rely on future/promise APIs, callbacks or async/await API.
Here we have a simple example of how we can create two fibers, one being nested inside the first one. These fibers will be executed concurrently based on when a fiber is suspended or finishes.
With Swoole we can do the same thing by executing two fibers/coroutines concurrently but we have to wrap our coroutines with a call to Web applications or servers contain a lot of different functionality and usually are responsible for performing many different tasks, they handle requests which itself is like a small program until it sends back a response. During the time one request is handled it would be beneficial if the server could also handle another request at the same time, concurrently. Allowing progress to be made on both requests simultaneously based on when one or another is waiting for I/O operations. This speeds up requests dramatically and allows the developer to still write synchronous code. We know fibers/coroutines are good for executing tasks concurrently in a synchronous manner but using them comes with some considerations in order to properly utilise fibers/coroutines:
Swoole supports the native CURL you have already known and all the PHP libraries based on CURL. The PHP Fibers RFC can only be used to make an HTTP Client with the use of a third-party PHP package. Let's take a look.
P.S. you can execute the above script with Swoole.
P.S. the script with PHP Fibers RFC can be executed is more complex. You have to learn and understand these new features and API in a non-strand third-party PHP library: FiberLoop, await, async. The last question for PHP developers to consider is: do you like to remove the native 1, 0, MySQL 3, 8 in your frameworks and applications, then use a third-party library written with PHP maybe providing the similar features of 1, 0, 3? |