国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP多線程編程之管道通信實例分析

2024-05-04 23:32:05
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP多線程編程之管道通信,實例分析了管道通信的原理與相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP多線程編程之管道通信用法。分享給大家供大家參考。具體分析如下:

一個線程如果是個人英雄主義,那么多線程就是集體主義,你不再是一個獨行俠,而是一個指揮家。

管道通信:

1. 管道可以認為是一個隊列,不同的線程都可以往里面寫東西,也都可以從里面讀東西。寫就是

在隊列末尾添加,讀就是在隊頭刪除。

2. 管道一般有大小,默認一般是4K,也就是內容超過4K了,你就只能讀,不能往里面寫了。

3. 默認情況下,管道寫入以后,就會被阻止,直到讀取他的程序讀取把數據讀完。而讀取線程也會被阻止,

直到有進程向管道寫入數據。當然,你可以改變這樣的默認屬性,用stream_set_block 函數,設置成非阻斷模式。

下面是我分裝的一個管道的類(這個類命名有問題,沒有統一,沒有時間改成統一的了,我一般先寫測試代碼,最后分裝,所以命名上可能不統一):

 

 
  1. <?php 
  2. class Pipe 
  3. public $fifoPath
  4. private $w_pipe
  5. private $r_pipe
  6.  
  7. /** 
  8. * 自動創建一個管道 
  9. * 
  10. * @param string $name 管道名字 
  11. * @param int $mode 管道的權限,默認任何用戶組可以讀寫 
  12. */ 
  13. function __construct($name = 'pipe'$mode = 0666) 
  14. $fifoPath = "/tmp/$name." . posix_getpid(); 
  15. if (!file_exists($fifoPath)) { 
  16. if (!posix_mkfifo($fifoPath$mode)) { 
  17. error("create new pipe ($name) error."); 
  18. return false; 
  19. else { 
  20. error( "pipe ($name) has exit."); 
  21. return false; 
  22. $this->fifoPath = $fifoPath
  23. /////////////////////////////////////////////////// 
  24. // 寫管道函數開始 
  25. /////////////////////////////////////////////////// 
  26. function open_write() 
  27. $this->w_pipe = fopen($this->fifoPath, 'w'); 
  28. if ($this->w_pipe == NULL) { 
  29. error("open pipe {$this->fifoPath} for write error."); 
  30. return false; 
  31. return true; 
  32.  
  33. function write($data
  34. return fwrite($this->w_pipe, $data); 
  35.  
  36. function write_all($data
  37. $w_pipe = fopen($this->fifoPath, 'w'); 
  38. fwrite($w_pipe$data); 
  39. fclose($w_pipe); 
  40.  
  41. function close_write() 
  42. return fclose($this->w_pipe); 
  43. ///////////////////////////////////////////////////////// 
  44. /// 讀管道相關函數開始 
  45. //////////////////////////////////////////////////////// 
  46. function open_read() 
  47. $this->r_pipe = fopen($this->fifoPath, 'r'); 
  48. if ($this->r_pipe == NULL) { 
  49. error("open pipe {$this->fifoPath} for read error."); 
  50. return false; 
  51. return true; 
  52. function read($byte = 1024) 
  53. return fread($this->r_pipe, $byte); 
  54. function read_all() 
  55. $r_pipe = fopen($this->fifoPath, 'r'); 
  56. $data = ''
  57. while (!feof($r_pipe)) { 
  58. //echo "read one K/n"; 
  59. $data .= fread($r_pipe, 1024); 
  60. fclose($r_pipe); 
  61. return $data
  62. function close_read() 
  63. return fclose($this->r_pipe); 
  64. /** 
  65. * 刪除管道 
  66. * 
  67. * @return boolean is success 
  68. */ 
  69. function rm_pipe() 
  70. return unlink($this->fifoPath); 
  71. ?> 
  72. /* 
  73. 有了這個類,就可以實現簡單的管道通信了。*/ 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清涧县| 乌拉特后旗| 武隆县| 红河县| 乾安县| 应用必备| 湟源县| 宁津县| 罗城| 清徐县| 溧阳市| 新龙县| 瓦房店市| 泰州市| 昌平区| 刚察县| 重庆市| 邛崃市| 河源市| 漠河县| 都江堰市| 民县| 罗甸县| 融水| 宣城市| 华蓥市| 丹凤县| 图木舒克市| 临夏县| 荆门市| 黔西县| 胶南市| 青川县| 靖安县| 张家港市| 敦化市| 商水县| 沈阳市| 淮南市| 清远市| 团风县|