search
HomeBackend DevelopmentPHP Tutorialphp开启多进程的方法_php技巧

本文实例讲述了php开启多进程的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

 $IP='192.168.1.1';//Windows電腦的IP
 $Port='5900';        //VNC使用的Port
 $ServerPort='9999';//Linux Server對外使用的Port
 $RemoteSocket=false;//連線到VNC的Socket
 function SignalFunction($Signal){
  //這是主Process的訊息處理函數
 global $PID;//Child Process的PID
 switch ($Signal)
 {
  case SIGTRAP:
  case SIGTERM:
   //收到結束程式的Signal
   if($PID)
   {
    //送一個SIGTERM的訊號給Child告訴他趕快結束掉嘍
    posix_kill($PID,SIGTERM);
    //等待Child Process結束,避免zombie
    pcntl_wait($Status);
   }
   //關閉主Process開啟的Socket
   DestroySocket();
   exit(0); //結束主Process
   break;
  case SIGCHLD:
   /*
當Child Process結束掉時,Child會送一個SIGCHLD訊號給Parrent
當Parrent收到SIGCHLD,就知道Child Process已經結束嘍 ,該做一些
結束的動作*/
   unset($PID); //將$PID清空,表示Child Process已經結束
   pcntl_wait($Status); //避免Zombie
   break;
  default:
 }
 }
 function ChildSignalFunction($Signal){
//這是Child Process的訊息處理函數
 switch ($Signal)
 {
  case SIGTRAP:
  case SIGTERM:
//Child Process收到結束的訊息
   DestroySocket(); //關閉Socket
   exit(0); //結束Child Process
  default:
 }
 }
 function ProcessSocket($ConnectedServerSocket){
 //Child Process Socket處理函數
 //$ConnectedServerSocket -> 外部連進來的Socket
 global $ServerSocket,$RemoteSocket,$IP,$Port;
 $ServerSocket=$ConnectedServerSocket;
 declare(ticks = 1); //這一行一定要加,不然沒辦法設定訊息處理函數。
//設定訊息處理函數
 if(!pcntl_signal(SIGTERM, "ChildSignalFunction")) return;
 if(!pcntl_signal(SIGTRAP, "ChildSignalFunction")) return;
//建立一個連線到VNC的Socket
 $RemoteSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//連線到內部的VNC
 @$RemoteConnected=socket_connect($RemoteSocket,$IP,$Port);
 if(!$RemoteConnected) return; //無法連線到VNC 結束
//將Socket的處理設為Nonblock,避免程式被Block住
 if(!socket_set_nonblock($RemoteSocket)) return;
 if(!socket_set_nonblock($ServerSocket)) return;
 while(true)
 {
//這邊我們採用pooling的方式去取得資料
  $NoRecvData=false;   //這個變數用來判別外部的連線是否有讀到資料
  $NoRemoteRecvData=false;//這個變數用來判別VNC連線是否有讀到資料
  @$RecvData=socket_read($ServerSocket,4096,PHP_BINARY_READ);
//從外部連線讀取4096 bytes的資料
  @$RemoteRecvData=socket_read($RemoteSocket,4096,PHP_BINARY_READ);
//從vnc連線連線讀取4096 bytes的資料
  if($RemoteRecvData==='')
  {
//VNC連線中斷,該結束嘍
   echo"Remote Connection Close\n";
   return;  
  }
  if($RemoteRecvData===false)
  {
/*
由於我們是採用nonblobk模式
這裡的情況就是vnc連線沒有可供讀取的資料
*/
   $NoRemoteRecvData=true;
//清除掉Last Errror
   socket_clear_error($RemoteSocket);
  }
  if($RecvData==='')
  {
//外部連線中斷,該結束嘍
   echo"Client Connection Close\n";
   return;
  }
  if($RecvData===false)
  {
/*
由於我們是採用nonblobk模式
這裡的情況就是外部連線沒有可供讀取的資料
*/
   $NoRecvData=true;
//清除掉Last Errror
   socket_clear_error($ServerSocket);
  }
  if($NoRecvData&&$NoRemoteRecvData)
  {
//如果外部連線以及VNC連線都沒有資料可以讀取時,
//就讓程式睡個0.1秒,避免長期佔用CPU資源
   usleep(100000);
//睡醒後,繼續作pooling的動作讀取socket
   continue;
  }
  //Recv Data
  if(!$NoRecvData)
  {
//外部連線讀取到資料
   while(true)
   {
//把外部連線讀到的資料,轉送到VNC連線上
    @$WriteLen=socket_write($RemoteSocket,$RecvData);
    if($WriteLen===false)
    {
//由於網路傳輸的問題,目前暫時無法寫入資料
//先睡個0.1秒再繼續嘗試。
     usleep(100000);
     continue;
    }
    if($WriteLen===0)
    {
//遠端連線中斷,程式該結束了
     echo"Remote Write Connection Close\n";
     return;
    }
//從外部連線讀取的資料,已經完全送給VNC連線時,中斷這個迴圈。
    if($WriteLen==strlen($RecvData)) break;
//如果資料一次送不完就得拆成好幾次傳送,直到所有的資料全部送出為止
    $RecvData=substr($RecvData,$WriteLen);
   }
  }
  if(!$NoRemoteRecvData)
  {
//這邊是從VNC連線讀取到的資料,再轉送回外部的連線
//原理跟上面差不多不再贅述
   while(true)
   {
    @$WriteLen=socket_write($ServerSocket,$RemoteRecvData);
    if($WriteLen===false)
    {
     usleep(100000);
     continue;
    }
    if($WriteLen===0)
    {
     echo"Remote Write Connection Close\n";
     return;
    }
    if($WriteLen==strlen($RemoteRecvData)) break;
    $RemoteRecvData=substr($RemoteRecvData,$WriteLen);
   }
  }
 }
 }
 function DestroySocket(){
//用來關閉已經開啟的Socket
 global$ServerSocket,$RemoteSocket;
 if($RemoteSocket)
 {
//如果已經開啟VNC連線
//在Close Socket前必須將Socket shutdown不然對方不知到你已經關閉連線了
  @socket_shutdown($RemoteSocket,2);
  socket_clear_error($RemoteSocket);
//關閉Socket
  socket_close($RemoteSocket);  
 }
//關閉外部的連線
 @socket_shutdown($ServerSocket,2);
 socket_clear_error($ServerSocket);
 socket_close($ServerSocket);
 }
//這裡是整個程式的開頭,程式從這邊開始執行
//這裡首先執行一次fork
 $PID=pcntl_fork();
 if($PID==-1) die("could not fork");
//如果$PID不為0表示這是Parrent Process
//$PID就是Child Process
//這是Parrent Process 自己結束掉,讓Child成為一個Daemon。
 if($PID) die("Daemon PID:$PID\n");
//從這邊開始,就是Daemon模式在執行了
//將目前的Process跟終端機脫離成為daemon模式
 if(!posix_setsid()) die("could not detach from terminal\n");
//設定daemon 的訊息處理函數
 declare(ticks = 1);
 if(!pcntl_signal(SIGTERM, "SignalFunction")) die("Error!!!\n");
 if(!pcntl_signal(SIGTRAP, "SignalFunction")) die("Error!!!\n");
 if(!pcntl_signal(SIGCHLD, "SignalFunction")) die("Error!!!\n");
//建立外部連線的Socket
 $ServerSocket=socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
//設定外部連線監聽的IP以及Port,IP欄位設0,表示經聽所有介面的IP
 if(!socket_bind($ServerSocket,0,$ServerPort)) die("Cannot Bind Socket!\n");
//開始監聽Port
 if(!socket_listen($ServerSocket)) die("Cannot Listen!\n");
//將Socket設為nonblock模式
 if(!socket_set_nonblock($ServerSocket)) die("Cannot Set Server Socket to Block!\n");
//清空$PID變數,表示目前沒有任何的Child Process
 unset($PID);
 while(true)
 {
//進入pooling模式,每隔1秒鐘就去檢查有沒有連線進來。
  sleep(1);
//檢查有沒有連線進來
  @$ConnectedServerSocket=socket_accept($ServerSocket);
  if($ConnectedServerSocket!==false)
  {
//有人連進來嘍
//起始一個Child Process用來處理連線
   $PID=pcntl_fork();
   if($PID==-1) die("could not fork");
   if($PID) continue;//這是daemon process,繼續回去監聽。
   //這裡是Child Process開始
   //執行Socket裡函數
   ProcessSocket($ConnectedServerSocket);
  //處理完Socket後,結束掉Socket
   DestroySocket();
  //結束Child Process
   exit(0);
  }
 }

希望本文所述对大家的php程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),