Asynchronous PHP – Multiprocessing, Multithreading and Coroutines

rKz3nGdYrV3Yq3f4i90uywZSWhXu6xLwbvxlD4HA

Let’s take a look at this typical PHP code:

 function names () { $data = Http::get( 'data.location/products' )-> json (); $names = []; foreach ($data as $item){ $names[] = $item[ 'name' ]; } return $names; }

We send an HTTP request that returns an array of items, then we store the name of each item in a $names array.

The time it takes to execute this function is equal to the duration of the request plus the time it took to build the array. What if we want to run this function multiple times for different data sources:

 $products = names( '/products'

The post Asynchronous PHP — Multiprocessing, Multithreading and Coroutines first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9541
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment