how to avoid 'out of memory' errors when p

2019-07-24 08:39发布

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?

2条回答
看我几分像从前
2楼-- · 2019-07-24 09:10

Yes, Batch API can solve this problem. It will break up your memory usage into separate HTTP requests, each with access to your full memory limit.

查看更多
劫难
3楼-- · 2019-07-24 09:18

have you ever used Views Bulk Operations? (http://drupal.org/project/views_bulk_operations) it comes with a bundled view displayed at admin/content/node2 you may edit that to enable a "Run PHP code" action, as well as turn on Batch API. it is the easiest way to programmaticaly modify nodes.

however, since you are creating the nodes, you should just unset the $node at the end of the instruction, and it should tone down your memory usage. try:

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

查看更多
登录 后发表回答