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)?
|
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 To answer the question 'Is it practible' it needs more information about the project requirements. Reasons for a 'yes' could be:
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. |
||||
|
There's already one built-in: http://php.net/manual/en/features.commandline.webserver.php
You don't usually do that in PHP, but rather hand it off to a sepparated service (which you can also write in PHP), like http://php.net/manual/en/book.gearman.php
This one depends on your needs and cannot be answered without more information. Python, Java or Golang may well be better alternatives Note: PHP is a multithreaded language, but it doesn't expose multithreading capabilities to the scripts' runtime. |
|||
|