Run PHP Task Asynchronously

后端 未结 15 1254
青春惊慌失措
青春惊慌失措 2020-11-22 15:15

I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don\'t want to m

15条回答
  •  庸人自扰
    2020-11-22 15:25

    It's a great idea to use cURL as suggested by rojoca.

    Here is an example. You can monitor text.txt while the script is running in background:

    \n";
        $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $url = preg_replace('/\?.*/', '', $url);
        $url .= '?begin='.$begin;
        echo 'URL: '.$url.'
    '; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); echo 'Result: '.$result.'
    '; curl_close($ch); } if (empty($_GET['begin'])) { doCurl(1); } else { while (ob_get_level()) ob_end_clean(); header('Connection: close'); ignore_user_abort(); ob_start(); echo 'Connection Closed'; $size = ob_get_length(); header("Content-Length: $size"); ob_end_flush(); flush(); $begin = $_GET['begin']; $fp = fopen("text.txt", "w"); fprintf($fp, "begin: %d\n", $begin); for ($i = 0; $i < 15; $i++) { sleep(1); fprintf($fp, "i: %d\n", $i); } fclose($fp); if ($begin < 10) doCurl($begin + 1); } ?>

提交回复
热议问题