how to avoid 'out of memory' errors when programmatically generating a lot of nodes in drupal?

Posted by sprugman on Stack Overflow See other posts from Stack Overflow or by sprugman
Published on 2010-04-27T21:56:10Z Indexed on 2010/04/28 1:33 UTC
Read the original article Hit count: 267

Filed under:
|

I'm creating about 150 nodes programmatically and running into 'out of memory' errors when doing it all in a single request. (I have a menu callback that generates the nodes and calls node_save() on them.)

Example:

for($i=0; $i<150; $i++) {
    $node = new stdClass(); 
    $node->title="Foo $i";
    $node->field_myfield[0]['value'] = "Bar $i";
    ...
    node_save($node);
}

I've heard of BatchAPI, but never used it. Is that the right tool to get around this? The docs talk about timeouts, but not memory issues. Is there something simpler that I might be missing?

© Stack Overflow or respective owner

Related posts about drupal

Related posts about nodes