gpt4 book ai didi

php - 如何通过优化我的 PHP 代码来减少虚拟内存?

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:52 25 4
gpt4 key购买 nike

我当前的代码(见下文)使用了 147MB 的虚拟内存!我的提供商默认分配了 100MB,进程一旦运行就被杀死,导致内部错误。该代码使用 curl multi,并且必须能够循环超过 150 次迭代,同时仍然最小化虚拟内存。下面的代码仅设置为 150 次迭代,仍然会导致内部服务器错误。在 90 次迭代时,问题不会发生。

如何调整我的代码以降低资源使用/虚拟内存?

谢谢!

<?php

function udate($format, $utimestamp = null) {
if ($utimestamp === null)
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}

$url = 'https://www.testdomain.com/';
$curl_arr = array();
$master = curl_multi_init();

for($i=0; $i<150; $i++)
{
$curl_arr[$i] = curl_init();
curl_setopt($curl_arr[$i], CURLOPT_URL, $url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_multi_add_handle($master, $curl_arr[$i]);
}

do {
curl_multi_exec($master,$running);
} while($running > 0);

for($i=0; $i<150; $i++)
{
$results = curl_multi_getcontent ($curl_arr[$i]);
$results = explode("<br>", $results);
echo $results[0];
echo "<br>";
echo $results[1];
echo "<br>";
echo udate('H:i:s:u');
echo "<br><br>";
usleep(100000);
}

?>

最佳答案

根据您最后的评论..

下载 RollingCurl.php

希望这能充分利用您的 API 发送垃圾邮件。

<?php

$url = '________';
$fetch_count = 150;
$window_size = 5;


require("RollingCurl.php");

function request_callback($response, $info, $request) {
list($result0, $result1) = explode("<br>", $response);
echo "{$result0}<br>{$result1}<br>";
//print_r($info);
//print_r($request);
echo "<hr>";
}


$urls = array_fill(0, $fetch_count, $url);

$rc = new RollingCurl("request_callback");
$rc->window_size = $window_size;
foreach ($urls as $url) {
$request = new RollingCurlRequest($url);
$rc->add($request);
}
$rc->execute();

?>

查看您的问题,我看到了 this comment :

If the intention is domain snatching, then using one of the established services is a better option. Your script implementation is hardly as important as the actual connection and latency.

我同意这个评论。

此外,您似乎已经发布了大约 七百次 的“相同问题”:

https://stackoverflow.com/users/558865/icer
https://stackoverflow.com/users/516277/icer

How can I adjust the server to run my PHP script quicker?
How can I re-code my php script to run as quickly as possible?
How to run cURL once, checking domain availability in a loop? Help fixing code please
Help fixing php/api/curl code please
How to reduce virtual memory by optimising my PHP code?
Overlapping HTTPS requests?
Multiple https requests.. how to?

您不得不一遍又一遍地问同样的问题这一事实是否告诉您 you're doing it wrong

你的 This comment:

@mario: Cheers. I'm competing against 2 other companies for specific ccTLD's. They are new to the game and they are snapping up those domains in slow time (up to 10 seconds after purge time). I'm just a little slower at the moment.

如果你真的想击败两家公司抢购过期域名,我相当确定在共享主机帐户上使用 PHP 是错误的工具。

关于php - 如何通过优化我的 PHP 代码来减少虚拟内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4567492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com