简体   繁体   中英

Do socket connections only need one thread?

As I understand, Apache isn't suited to serving long-poll requests, as each request into Apache will use one worker thread until the request completes, which may be a long time for long-poll/COMET requests.

But what about socket connections. On the PHP website I saw an example of a "simple multi-client server written in PHP that really works" .

My question: Does such socket servers only use one worker thread for all established connections? And what about the opposite: Is it possible to write a PHP client which connects to several socket servers simultaneously using only one worker thread?

Look at phpDaemon . It design for long-pool applications and similar. But I advise you to use node.js for these tasks, if possible.

That is an example of a polling-loop style server - see the MSG_DONTWAIT constant being passed to socket_recv() ? Essentially, it has a single thread that loops through all of its open sockets to see if any of them has data waiting. If a socket doesn't have data waiting, it moves on to the next and checks it.

However, note that with such a server, you don't get nice protocol handling beyond the TCP base - you have to worry about parsing a stream of raw data yourself.

All of your connections are done with sockets. The main difference is whether I/O is blocking or not. Choosing to receive from a socket that blocks will cause the thread to block, but using MSG_DONTWAIT will finish immediately.

Apache gives you a few options in this regard. You can fork for concurrent connections (mpm-prefork), use a different thread for each connection (mpm-worker), or threads with non-blocking I/O (mpm-event).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM