Is it possible (and practicable) to develop a (simple) server software using the PHP CLI SAPI? Is there some usable way of (real) multi-threading in PHP so the server is able to handle several request simultaneously? Or would you recommend another Scripting language for a project like this (Python or something like that)?
2条回答 默认 最新
- dpqg86714 2012-12-14 14:14关注
Yes it is possible to develop a TCP or UDP server in PHP. Have a look at the set of socket functions in PHP: PHP:Sockets - Manual
Although PHP has no multithreading capabilities you can create simple parallelism using non blocking IO (see
socket_set_nonblock()
) along withsocket_select()
To answer the question 'Is it practible' it needs more information about the project requirements. Reasons for a 'yes' could be:
- You need to integrate the server into an existing PHP class, model framework
- You plan to interexchange PHP data structures using the server. (Might be generated using
serialize()
,unserialize()
)
You may find additional reasons for you of course.
Another thing you should note is that when in web server environment (e.g apache SAPI) you already have a parallel TCP server - the web server. You would just have to implement the communication between several requests. You may use PHP's IPC capabilities, a database or at least a file for that communication.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报