Is PHP not suitable for the Internet of Things server?
In traditional thinking, people often tell you that PHP is not suitable for IoT servers and ask you to change to Java or node. , go and other languages, yes, yes, PHP in the traditional sense is indeed difficult to make an Internet of Things server because it is so lame. Of course, this does not mean that it cannot be done at all. (Recommended learning: PHP video tutorial)
For example, when you want to implement a TCP server, you may need to write code with the following principle:
for ($i = 0;$i <= 1;$i++){
$pid = pcntl_fork();
if($pid){
if($i == 0){
$server = stream_socket_server("tcp://127.0.0.1:9501", $errno, $errstr, STREAM_SERVER_BIND);
}else if($i == 1){
$tickTime = time()+3600;
while (1){
usleep(1);
if($tickTime == time()){
//do my tick func
}
}
}
}}
Copy after login
The meaning of the above code is equivalent to creating a TCP server in one process, and doing time detection in an infinite loop in another process, thereby realizing the timer logic.
This looks really lame, and for PHPer whose programming foundation is generally weak, it is really difficult to maintain. Of course, at this time, some people will say, isn’t there Workerman? Yes, there is indeed Workerman. Workerman is a PHP multi-process framework that highly encapsulates the above code principles and helps you focus on implementing code logic. Therefore, it is said that PHP Doing the Internet of Things from time to time is actually a fallacy.
Of course, some people may say at this time that the Go language has coroutines. When you use Workerman to block database calls, the efficiency will be very poor and it will be difficult to achieve high concurrency. That is correct. But in fact, we can use multiple processes as much as possible to make up for this shortcoming, that is, a heap machine. Of course, if you really want to spend every penny, it doesn't matter. At this time, we can come up with our killer weapon, which is the coroutine of
Swoole4.x.
The above is the detailed content of Can php do the Internet of Things?. For more information, please follow other related articles on the PHP Chinese website!