php多線程求答疑

來源:互聯網
上載者:User
多線程 php

在網上看到一些demo,很多一樣的,自己拿到本機測試,出了點問題:
//a.php
function runThread(){
$fp = fsockopen('localhost', 80, $errno, $errmsg);
fputs($fp, "GET /a.php?act=b");
fclose($fp);
}

function a(){
$fp = fopen('result_a.log', 'w');
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "\r\n");
fclose($fp);
}

function b(){
$fp = fopen('result_b.log', 'w');
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "\r\n"); fclose($fp);
}

if(!isset($_GET['act'])) $_GET['act'] = 'a';

if($_GET['act'] == 'a'){
runThread();
a();
}
else if($_GET['act'] == 'b') b();
?>

每次我訪問localhost/a.php時,只能在result_a.log中寫入記錄,result_b.log中從來不會寫入記錄,我確定我的php環境中已經可以啟用fsocket擴充了的,求高手指點一下


回複討論(解決方案)

自挽一下,求大哥們指點

因為你預設的$_GET['act'] 是 執行 a()函數
執行localhost/a.php?act=b試試

php的多線程?

應該是fputs裡面的資料包頭部些少了什麼造成的。
我的意見:
1 使用firebug看看就知道應該寫什麼,觀察你請求的包頭
2 放棄fsock系列函數,很多地方都不支援這個,不過你可以使用curl來類比你的請求。

使用firebug看看錯誤,或者用echo 檢查程式執行到哪了?沒執行到哪一步一步的檢查。

好吧,該結貼了

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.