socket-php Pthreads How does the main thread get scoket resources to child threads?

Source: Internet
Author: User
How does the main thread of PHP pthreads get scoket resources to child threads?
A recent business to do is to use the AMQP consume method to listen to the queue, while maintaining a connection with multiple terminals, once there is a message in the queue immediately sent to the terminal, there can be no delay,
I think of is only to use the thread to do, if the use of multi-process to achieve, the socket resources can not be shared, but now with the pthreads extension is not, from the main thread can not get to the child thread connection, the heroes ask how to get

  
  Connections = Array ();        Public Function Run () {$connections = array ();        Listening Port $PORT = 8888;        $sock = Socket_create_listen ($PORT);        if (! $sock) {exit (1);        }//does not block Socket_set_nonblock ($sock);            while (true) {//$readfds = Array_merge ($this->connections, Array ($sock));            $readfds = Array_merge ($connections, Array ($sock));            $writefds = Array ();            Select a connection to get the read, write connection channel $e = NULL;            $flag = Socket_select ($readfds, $writefds, $e, $t = 5); if ($flag) {foreach ($readfds as $RFD) {if ($rfd = = $sock) {//                        Accept client Connections $newconn = socket_accept ($sock);                        $i = (int) $newconn;                        Place the current client connection as Socket_select select $connections [$i] = $newconn;         Enter the connection resource cache container $writefds [$i] = $newconn;               Var_dump ($connections);                        $this->connections = $connections;                        Var_dump ($this->connections);                        echo "Welcome to the PHP Chat server!\n";                    Continue        }}} echo "a\n";   }}} $my = new PrintServer (); $my->start (); while (true) {Var_dump ($my->connections); Sleep (3);}

Reply content:

How does the main thread of PHP pthreads get scoket resources to child threads?
A recent business to do is to use the AMQP consume method to listen to the queue, while maintaining a connection with multiple terminals, once there is a message in the queue immediately sent to the terminal, there can be no delay,
I think of is only to use the thread to do, if the use of multi-process to achieve, the socket resources can not be shared, but now with the pthreads extension is not, from the main thread can not get to the child thread connection, the heroes ask how to get

  
  Connections = Array ();        Public Function Run () {$connections = array ();        Listening Port $PORT = 8888;        $sock = Socket_create_listen ($PORT);        if (! $sock) {exit (1);        }//does not block Socket_set_nonblock ($sock);            while (true) {//$readfds = Array_merge ($this->connections, Array ($sock));            $readfds = Array_merge ($connections, Array ($sock));            $writefds = Array ();            Select a connection to get the read, write connection channel $e = NULL;            $flag = Socket_select ($readfds, $writefds, $e, $t = 5); if ($flag) {foreach ($readfds as $RFD) {if ($rfd = = $sock) {//                        Accept client Connections $newconn = socket_accept ($sock);                        $i = (int) $newconn;                        Place the current client connection as Socket_select select $connections [$i] = $newconn;         Enter the connection resource cache container $writefds [$i] = $newconn;               Var_dump ($connections);                        $this->connections = $connections;                        Var_dump ($this->connections);                        echo "Welcome to the PHP Chat server!\n";                    Continue        }}} echo "a\n";   }}} $my = new PrintServer (); $my->start (); while (true) {Var_dump ($my->connections); Sleep (3);}
  • 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.