Home > Article > Backend Development > PHP generator yield handles large amounts of data business (code example)
Official explanationyield
yield generator
is# Appeared after ##php5.5, the official document explains this:
yield provides an easier way to implement simple iteration objects, compared to defining a class to implement
Iterator Interface approach, performance overhead and complexity are greatly reduced.
yield keyword. A generator function looks like an ordinary function. The difference is: an ordinary function returns a value, while a generator can
yieldGenerates as many values as it needs. When the generator function is called, it returns an object that can be iterated over.
yield is somewhat similar to
return, but the difference is that
return will return a value and terminate the execution of the code, while
yield will return a value to the code that calls this generator in a loop and just pause execution of the generator function.
Here I would like to introduce to you the PHP version of non-buffered query
It means reading the data line by line into the PHP running memory. It is not a one-time read into the PHP running memory. As we all know, PHP has many built-in functions that can help us process the data. Because the data is in the memory, it can be operated, but the PHP running memory has a limit, and the default is 128M. Note: Because non-buffered queries will take a long time to connect to the database, it may cause slow queries, table locks, etc., which consume more mysql resourcesrelative to non-buffered queries The query is a buffered query:
If you use cached query, the PHP memory will directly explode and there will be insufficient memory. Okay, the main purpose here is to highlight yieldyield performance
The generator will have a very large impact on the performance of PHP applicationsSaving a lot of memory when PHP code is runningMore suitable for calculating large amounts of datayield operationUse
Generators allow you to write code in a foreach block to iterate over a set of data without creating an array in memory, which would hit your memory limit or take up considerable processing time. Instead, you can write a generator function, just like a normal custom function, and instead of a normal function returning only once, the generator can yield as many times as needed to generate the values that need to be iterated over. Example explanation I just constructed an array to demonstrate to you, usually you The same goes for operating database output data. Convert to array//仓库库存扣除测试 public function cangku_stock() { //set_time_limit(0); //表示永久运行,这里我是测试array的时候用到的 $order_info = $this->read_temp_api_order_info(10); //这里我就测试了10条数据,效果是看不出来的 foreach($order_info as $temp_api_order_info){ dd($temp_api_order_info); //打印出来看看数据 //处理数据 $api_ware_id = $this->o->getCangkuApiUrl() .'ware/program/addOutWare'; $out_wares = api_request($api_ware_id, $temp_api_order_info); $temp_out_wares = json_decode($out_wares, true); if ($temp_out_wares['code'] != 1) { $msg = (isset($temp_out_wares['msg']) && $temp_out_wares['msg']) ? $temp_out_wares['msg'] : var_export($out_wares, true); throw new Exception($msg); } } //dd("批量更新成功".date('Y-m-d H:i:s'));You can see that we called $order_info = $this->read_temp_api_order_info(10); and returned a Generator object. This object can be iterated using foreach. For each iteration, PHP will ask for a Generator instance. Calculate and provide the next value to iterate over. The elegance of the generator is reflected in the fact that each time a value is produced, the internal state of the generator will pause; when the next value is requested from the generator, the internal state will be restored. The generator's internal state switches between stalling and resuming until the end of the function definition is reached or an empty return statement is encountered. The effect is as follows: Test a large amount of data here, just change
$this->read_temp_api_order_info(10); If you are calculating the number of data tables, then you need to change this method. Try changing it yourself.
php tutorial!
The above is the detailed content of PHP generator yield handles large amounts of data business (code example). For more information, please follow other related articles on the PHP Chinese website!