我的php腳本使用php simplehtmldom來解析html,並獲取我想要的所有鏈接和圖像,這可以運行一段時間,具體取決於要下載的圖像數量。PHP:使用POST/AJAX取消運行腳本?
我認爲在這種情況下允許取消是個好主意。目前我使用Jquery-Ajax調用我的php,我能找到的最接近的是php register_shutdown_function,但不知道它是否適用於我的情況。有任何想法嗎?
所以一旦PHP啓動,它不能被打擾?像再次調用ajax來調用一個出口到同一個php文件?
我的php腳本使用php simplehtmldom來解析html,並獲取我想要的所有鏈接和圖像,這可以運行一段時間,具體取決於要下載的圖像數量。PHP:使用POST/AJAX取消運行腳本?
我認爲在這種情況下允許取消是個好主意。目前我使用Jquery-Ajax調用我的php,我能找到的最接近的是php register_shutdown_function,但不知道它是否適用於我的情況。有任何想法嗎?
所以一旦PHP啓動,它不能被打擾?像再次調用ajax來調用一個出口到同一個php文件?
只有在您通過AJAX處理真正的海量數據加載時,這纔是好事。對於其他情況,只需在JS中處理它即可取消顯示結果。
但正如我所說如果你正在處理大量的數據,那麼你可以在運行腳本的每第n步添加一箇中斷條件,並使用另一個腳本來滿足該條件。例如,您可以使用文件來存儲中斷數據或MySQL MEMORY表。
例子。
1,process.php(AJAX腳本處理數據的負載)
// clean up previous potential interrupt flag
$fileHandler = fopen('interrupt_condition.txt', 'w+');
fwrite($fileHandler, '0');
fclose($fileHandler);
function interrupt_check() {
$interruptfile = file('interrupt_condition.txt');
if (trim($interruptfile[0]) == "1") { // read first line, trim it and parse value - if value == 1 interrupt script
echo json_encode("interrupted" => 1);
die();
}
}
$i = 0;
foreach ($huge_load_of_data as $object) {
$i++;
if ($i % 10 == 0) { // check for interrupt condition every 10th record
interrupt_check();
}
// your processing code
}
interrupt_check(); // check for last time (if something changed while processing the last 10 entries)
2,interrupt_process.php(AJAX腳本傳播取消事件到文件)
$fileHandler = fopen('interrupt_condition.txt', 'w+');
fwrite($fileHandler, '1');
fclose($fileHandler);
這肯定會影響表現你的腳本,但會讓你成爲關閉執行的後門。這是非常簡單的例子 - 你需要使它更加複雜,使之爲更多的用戶同時工作,等
您還可以使用MySQL的內存表,MEMCACHE - 非持久緩存服務器或任何非 - 您可以找到的持久存儲。