Home > Backend Development > PHP Tutorial > PHP generator yield handles large amounts of data business (code example)

PHP generator yield handles large amounts of data business (code example)

藏色散人
Release: 2023-04-08 10:10:01
forward
5533 people have browsed it

PHP generator yield handles large amounts of data business (code example)

Official explanationyield

yield generatoris# 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.

The core of a generator is a

yield keyword. A generator function looks like an ordinary function. The difference is: an ordinary function returns a value, while a generator canyieldGenerates 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 resources

relative 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 yield

yield performance

The generator will have a very large impact on the performance of PHP applications

Saving a lot of memory when PHP code is running

More suitable for calculating large amounts of data

yield 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

PHP generator yield handles large amounts of data business (code example)

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'));
Copy after login

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:

PHP generator yield handles large amounts of data business (code example)

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 generator yield handles large amounts of data business (code example)

Here I will mainly explain to you how to use yield. If you want to see how long it takes to insert the data table, you can You can add an insertion time field to the data table, and then look at the comparison between the first data insertion and the last data insertion.

For more related php knowledge, please visit

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template