Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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)?

share|improve this question
    
You seem to be looking for a solution looking for a problem... – Ignacio Vazquez-Abrams Apr 17 '12 at 18:29
    
nope, i'm just asking if it's possible in php without huge effort. If not, i would switch to phyton or something – Stefan Apr 17 '12 at 18:30
    
Running PHP via CLI from a web server gains you nothing over CGI or mod_php. – Ignacio Vazquez-Abrams Apr 17 '12 at 18:32
1  
I've done it many times. PHP + libevent is what you need. – strkol Apr 17 '12 at 18:51
1  
@Stefan, you can't do multi-threading in PHP, because unfortunately it doesn't support threads. But with php+libevent you can handle many connections in one thread/process. The downside is that if you do blocking operations (fread/sockets/etc), the other connections will freeze until you finish with the operation. If you really need to do blocking operations you can fork a new process for each connection, but it's slower. Choosing the right solution depends on your needs. – strkol Apr 17 '12 at 19:08

2 Answers 2

up vote 1 down vote accepted

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 (seesocket_set_nonblock()) along with socket_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.

share|improve this answer

Is it possible (and practicable) to develop a (simple) server software using the PHP CLI SAPI?

There's already one built-in: http://php.net/manual/en/features.commandline.webserver.php

Is there some usable way of (real) multi-threading in PHP so the server is able to handle several request simultaneously?

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

Or would you recomend another Scripting language for a project like this (Phyton or something like that) ?

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.