Re: [請益] PHP有沒有辦法送出請求後,不等待回應 …
1 <?php
2 for( $i = 5 ; $i < 20 ; $i++){
3 exec("/bin/sleep $i > /dev/null 2>&1 &");
4 }
5 ?>
用網頁 request 這個 php 頁面
瀏覽器不會等待 php 結束
在伺服器上面用 ps -ef | grep "sleep" 查找的話
也可以找到很多隻 sleep 在執行
> /dev/null 2>&1 &
上面這行是有這樣效果的原因
※ 引述《Jerr (身為老爸老媽嚕..)》之銘言:
: POSIX的platform可以用pcntl_fork(),
: http://tw2.php.net/manual/en/function.pcntl-fork.php
: 要注意要找機會 wait 或 waitpid, 不然 child exit會變成zombie process.
: 複製sample code:
: $pid = pcntl_fork();
: if ($pid == -1) {
: die('could not fork');
: } else if ($pid) {
: // we are the parent
: pcntl_wait($status); //Protect against Zombie children
: } else {
: // we are the child
: }
: ※ 引述《asika32764 (飛鳥 Asika)》之銘言:
: : 最近想再執行網頁程式的時候,
: : call一些網站頁面、圖片、或是伺服器上的檔案執行工作。
: : 希望能讓伺服器繼續背景執行,但是不要鎖住瀏覽器。
: : 可是我發現無論用 file_get_contents()等內建函式,
: : 還是用system()與exec() call主機功能運行,都還是會在等待回應的時間
: : 鎖著瀏覽器無法繼續。
: : 想請問PHP有辦法避開等待回應的時間繼續執行嗎?
: : 類似AJAX這樣,真的不行就得要很蠢的用JS來做了...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.147.15.66
※ 編輯: iwhiori 來自: 122.147.15.66 (12/28 15:53)
→
12/28 15:56, , 1F
12/28 15:56, 1F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章