如何避免“内存不足”在drupal中以编程方式生成大量节点时出错?

发布于 2024-08-30 04:13:33 字数 380 浏览 4 评论 0原文

我以编程方式创建了大约 150 个节点,并在单个请求中完成所有操作时遇到“内存不足”错误。 (我有一个菜单回调,可以生成节点并在它们上调用node_save()。)

示例:

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

我听说过 BatchAPI,但从未使用过它。这是解决这个问题的正确工具吗?文档讨论了超时,但没有讨论内存问题。有什么更简单的东西我可能会错过吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼泪 2024-09-06 04:13:34

是的,Batch API可以解决这个问题。它将您的内存使用情况分解为单独的 HTTP 请求,每个请求都可以访问您的全部内存限制。

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.

澜川若宁 2024-09-06 04:13:34

你用过视图批量操作吗? (http://drupal.org/project/views_bulk_operations)
它带有一个显示在 admin/content/node2 的捆绑视图
您可以编辑它以启用“运行 PHP 代码”操作,以及打开批处理 API。
这是以编程方式修改节点的最简单方法。

但是,由于您正在创建节点,因此您应该在指令末尾取消设置 $node,并且它应该会降低内存使用量。尝试:
<代码>

  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);
  }
}

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);
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文