PHP socket model and efficiency issues

Source: Internet
Author: User

//Creating Socketssocket_create ();//bindingSocket_bind ();//MonitorSocket_listen ();//body, dead loop while(true){  //Select model, remove the list of readable socketsSocket_select (); foreach(sockets) {//If the socket is listening on the connection request Port    if( islistensocket) {      //Accept Requestsocket_accept (); }    Else {      //Read PacketsSocket_recv (); //handling user Actions {The problem is here}process (); }  }}
PHP Socket model and efficiency issues3ticket, Hoo -so. -//Creating Socketssocket_create ();//bindingSocket_bind ();//MonitorSocket_listen ();//body, dead loop while(true){  //Select model, remove the list of readable socketsSocket_select (); foreach(sockets) {//If the socket is listening on the connection request Port    if( islistensocket) {      //Accept Requestsocket_accept (); }    Else {      //Read PacketsSocket_recv (); //handling user Actions {The problem is here}process (); }}} about the efficiency of PHP as a server, I think this is a problem. The entire loop is blocked at the time of each user's action, causing other connections to be unacceptable, and other users ' action requests cannot be processed. The next action cannot be processed until the process () function returns. When dealing with a user action, it may be time-consuming to involve database access, and so on. I wonder if there is any solution to the great fairy. I think that if you can do an action queue, where the loop is only responsible for receiving data, and then the packet is persisted in a queue, to receive the next user's packet. Another thread (does not know how PHP implements the thread, or can implement it) extracts each user's action request from the queue to handle it. or the process () function can be made non-blocking, allowing it to return immediately. Younger brother Beginners, if there is a ridiculous place, please forgive me! Comments (0) • Share • link • -- -- -4 Answers Ticket number Feng Yijun1ticket Feng Yijun 14.02K The best answer to this question I have also encountered, such as open two terminal, Telnet socket server, if one does not return, the other terminal is always waiting. Finally, using the fork process to solve the problem, probably the code below, you can refer to the following: Dead Loop part Do{$msgsock=socket_accept ($socket); $pid=pcntl_fork (); if($pid = =-1) {                //Fork Error;}Else if($pid) {socket_close ($msgsock); }Else{$buf= Socket_read ($msgsock,1024x768, Php_normal_read); $ret= Func ($BUF);//Call function to process received contentSocket_write ($msgsock, $ret, strlen ($ret));                Socket_close ($msgsock); //Posix_kill (posix_getpid (), 0);exit (); }        } while(true) Reviews (Ten) • links • -- -- -0@ Feng Yijun Hello, I am in the local test, you can use CMD to run PHP, or on the Web page to access, and then close the page, PHP will continue to execute, and then the port can be accessed all the time. But I put on the server (Linux), how to let it continue to execute AH, I use the Web page to access it when it can be connected, if the page closed, it will be closed. – Hoo-Ann -- -- -0@ hoo-Ann adds Ignore_user_abort at the beginning of the socket server program (1); Set_time_limit (0); These two sentences, you can run the browser once. You can also run at the command line, such as/usr/local/php/bin xxx.php &– Feng Yijun -- -- -0@ Feng Yijun set_time_limit (0This I added, Ignore_user_abort (1); This one doesn't add up. -- -- -0@ ignore_user_abort (); The main is to set the line with the client disconnects whether the script will be terminated, Set_time_limit () is mainly to specify the maximum time the program runs. – Feng Yijun -- ,- GenevaShow more hidden comments by hoo-Ann2votes 70 I seem to have thought of a way. Modify the process () and write it directly into the database (or who has a quicker way to tell). is what I call the "action queue". Write a similar php file, also a dead loop.  while(true) {//remove a pending action from the database//Handle It//Delete the action from the databaseYou can even run this file several times, just don't know how to stop when you want to stop.>_<Comments (0) • links • -- -- -Lobster Cat0Tickets Lobster Cat 1 wind big do a pcntl based multi-process Socket service framework, you can refer to the use of the following http://code.google.com/p/mpass/

PHP socket model and efficiency issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.