Home  >  Article  >  Backend Development  >  How to use multithreading in php?

How to use multithreading in php?

WBOY
WBOYOriginal
2016-12-01 00:01:161092browse

Business scenario: Obtain data from the API interface and insert it into the database. This process takes about a few hours. I want to use multi-threading technology to transform it, but I don’t know how to start. Other ways will work too.

One more thing: I don’t know the future direction of PHP. Java has many heavyweight things, such as hadoop, spark, storm, etc. Where will PHP go from here?

Reply content:

In fact, what you really need here is just to find a way on the Web to perform an "asynchronous" task that lasts for several hours. As far as PHP is concerned, there are two ways to do it:
(1) Use the shell_exec function in PHP to start an independent PHP script execution in the shell. This method is actually equivalent to setting up a separate shell process to handle your tasks during the processing of the web server. Here, special attention needs to be paid to the server security of shell_exec. Pay attention to verifying parameters and be careful to avoid being brought into the shell command. This is a relatively easy way to implement.
(2) Use PHP to implement a Server, listen on a port, and provide services for the Web side. There are many implementation methods here, usually with extensions, such as native pthread (multi-threading), open source extension swoole, etc.

It is inappropriate to use multi-threading directly in web programs. Web requests usually have time limits. For example, the default timeout is 30 seconds. How do you ensure that before the thread task is completed, its parent process is still working? swoole Okay, Swoole: PHP’s asynchronous, parallel, distributed extension, solve your problem You can use the command line mode of php to run, but this is not multi-threading, but multi-process.
For a more complex way, you can use gearman. Multiple processes can achieve this effect. PHP itself does not support multi-threading, but some extensions can help you do it Let me share my opinion.
This scenario does not necessarily need to be solved with multi-threading. Using multiple processes is more stable.
Then in business scenarios. Insert into database. What kind of database is it? Isn't it because of the write lock problem?
PHP can use processes, threads, asynchronous, etc. There are many extensions written by experts that can help you do it. As for the performance in business scenarios, you have to test it yourself.
As for PHP, you can learn more about what others mean by fast and slow, and the improvements that have come out in 7. At the same time, PHP is mainly a web development script. You need to understand the meaning of comparison.
Also, experts generally don’t only speak one language. Or it means not sticking to one language.
all in interesting.
I hope you guys can point out the bad points.
------------------------------------------------
After reading the latest answer, I would like to add that, actually, I also I want to say 'asynchronous' solution is fine. 2333 You can use the extension php_pthreads for multi-threading. The advantage is that it supports both WIN and lin platforms. The disadvantage is that php must be run in thread-safe mode. Don't think about running it in CGI mode. For simple asynchronous, you can consider popen(), fsockopen(), cURL;
For more complex ones, you can consider PCNTL/pthreads extension;
For more complex ones, consider message queue -- background service;
And swoole! First, locate the bottleneck of the program, which must be mysql. Whether parallelism can solve this problem depends on your specific storage structure.
Solution:
1. Write mysql in batches, which is much faster than writing in parallel
2. Use the php multi-process framework, GitHub - huyanping/simple-fork-php: simple multi process manager based on pcntl Provides threads like java same interface
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