Home  >  Article  >  Backend Development  >  Will php be used for the Internet of Things?

Will php be used for the Internet of Things?

(*-*)浩
(*-*)浩Original
2019-09-10 10:36:033899browse

Isn’t PHP suitable for the Internet of Things server?

Will php be used for the Internet of Things?

In traditional thinking, people often tell you that PHP is not suitable for IoT servers. Let you switch to other languages ​​​​such as Java, node, go, etc. Yes, it is true that 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 completely.

For example, when you want to implement a TCP server, you may need to write code with the following principle: (Recommended learning: PHP programming from entry to proficiency )

for ($i = 0;$i <= 1;$i++){<br/>    $pid = pcntl_fork();<br/>    if($pid){<br/>        if($i == 0){<br/>            $server = stream_socket_server("tcp://127.0.0.1:9501", $errno, $errstr, STREAM_SERVER_BIND);<br/>        }else if($i == 1){<br/>            $tickTime = time()+3600;<br/>            while (1){<br/>                usleep(1);<br/>                if($tickTime == time()){<br/>                    //do my tick func<br/>                }<br/>            }<br/>        }<br/>    }}<br/>

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 highly encapsulates the above code principles and helps you focus on implementation. The code logic is a PHP multi-process framework, so it is a fallacy to say that PHP does not always do the Internet of Things.

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. So Yes, 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, that is the coroutine of Swoole4.x.

Actor Model

What is Actor? Simply put, Actor is a highly abstracted concurrency model. Each Actor instance The memory spaces are isolated from each other to reduce the difficulty of user programming and maintenance.

The above is the detailed content of Will php be used for the Internet of Things?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn