note 124334 added to generator.send
| From: | baohx2000 at gmail dot com | Date: | Thu, 24 Oct 2019 19:15:53 +0000 |
| Subject: | note 124334 added to generator.send | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212762@lists.php.net to get a copy of this message | ||
I have found that inverse generators (using $x = yield) is a great way to handle chunked batch
processing. As data is being iterated, once a specific count has been fed to the generator, it
processes and resets the data. For example, you could do a batch mysql insert every 500 records.
Example (note the handling of null, which you would send to the generator to handle stragglers after
the previous batch)
function importer()
{
$max = 500;
$items = [];
while (true) {
$item = yield;
if ($item !== null) {
$items[] = yield;
}
if ($item === null || count($items) >= $max) {
// do batch operations
$items = [];
}
}
}
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.54 (proxied: 64.125.67.66)
----
Manual Page -- http://php.net/manual/en/generator.send.php
Edit -- https://master.php.net/note/edit/124334
Del: integrated -- https://master.php.net/note/delete/124334/integrated
Del: useless -- https://master.php.net/note/delete/124334/useless
Del: bad code -- https://master.php.net/note/delete/124334/bad+code
Del: spam -- https://master.php.net/note/delete/124334/spam
Del: non-english -- https://master.php.net/note/delete/124334/non-english
Del: in docs -- https://master.php.net/note/delete/124334/in+docs
Del: other reasons-- https://master.php.net/note/delete/124334
Reject -- https://master.php.net/note/reject/124334
Search -- https://master.php.net/manage/user-notes.php