我正试图通过几乎立即超时的卷曲请求启动长期运行脚本的“异步”php工作。我知道存在不同堆栈的更好的解决方案,但这似乎是一个常见的建议,当vanilla php完全可用时。
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); // Follow the redirects (needed for mod_rewrite)
curl_setopt($c, CURLOPT_HEADER, false); // Don't retrieve headers
curl_setopt($c, CURLOPT_NOBODY, true); // Don't retrieve the body
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); // Return from curl_exec rather than echoing
// Timeout super fast once connected, so it goes into async.
curl_setopt( $c, CURLOPT_TIMEOUT_MS, 1 );
return curl_exec( $c );
在目标$url
中我定义了一个应该忽略用户中止的脚本,然后自己动手。
ob_start();
ignore_user_abort(true);
set_time_limit(0);
echo "OK";
ob_flush();
flush();
// wait 5s to help debug
sleep(5);
// processing stuff
因此CURLOPT_TIMEOUT_MS
设置为1ms
,脚本永远不会运行。将其设置为100ms
,它仍然永远不会运行。将其设置为500ms
,它以预期的行为运行(父脚本放弃并在~500ms后返回,我的脚本的预期逻辑在5s
之后执行。
我做错了什么,或者是简单的答案,即使脚本顶部的ignore_user_abort()
需要一定的时间来执行?