How does PHP handle two threads in parallel?

Source: Internet
Author: User
1. The user uploads a picture to the receive.php of a server,
2. After the receive.php of a server receives this image, it will post this image to B server via curl,
(Because the picture is very large, it is time-consuming to transfer pictures to the B server)
In this long-time-consuming process, If I send a request request2 to the receive.php of a server again, but because this receive.php is also transferring the picture to the B server, the request waits until the a server has completely sent the picture, and then comes back to process the second request request2.

Question: Now how to break this queue effect, let a server receive.php processing two requests at the same time?
Even if you can't implement PHP multithreading, then how can let Request2 do pseudo-request, I mean, assuming request2 request receive.php, receive.php to query a large number of databases, and finally echo "Request Done", Now I just need to output echo "request Done" and the rest of the receive.php.

Reply content:

1. The user uploads a picture to the receive.php of a server,
2. After the receive.php of a server receives this image, it will post this image to B server via curl,
(Because the picture is very large, it is time-consuming to transfer pictures to the B server)
In this long-time-consuming process, If I send a request request2 to the receive.php of a server again, but because this receive.php is also transferring the picture to the B server, the request waits until the a server has completely sent the picture, and then comes back to process the second request request2.

Question: Now how to break this queue effect, let a server receive.php processing two requests at the same time?
Even if you can't implement PHP multithreading, then how can let Request2 do pseudo-request, I mean, assuming request2 request receive.php, receive.php to query a large number of databases, and finally echo "Request Done", Now I just need to output echo "request Done" and the rest of the receive.php.

Think you need a message queue.

In addition, you can:

To be reproduced. The PHP language itself does not support multithreading. summed up the online on the PHP simulation of multi-threaded methods, in general, are the use of PHP's good partners themselves have the multithreading capabilities. PHP's good partner is Linux and Apache, lamp. In addition, since it is analog, it is not really multi-threaded. It's just a multi-process. Processes and threads are two different concepts. Well, the following methods are found on the Internet. 1. Using the Linux operating system
  Save it as test.php, then write a shell code #!/bin/bashfor i in 1 2 3 4 5 6 7 8 9 10dophp-q test.php &done2. Leveraging the Fork sub-process (in fact, using the Linux operating system)
  " . Time (). "$str n"; exit ();//Sub-process Code snippet _end}}if ($bWaitFlag) {for ($i = 0; $i < $intNum; $i + +) {pcntl_waitpid ($pids [$i], $status, WU ntraced); echo "Wait $i." Time (). "N";}} Echo ("Endn");? >3. With Web SERVER, PHP does not support multi-threading, Apache is supported, hehe. Let's say we're running the a.php document. But I also ask the Web server to run another b.php in the program. Then the two documents will be executed concurrently.
  Of course, you can also take the multi-threading parts to Java to deal with, and then call in PHP, haha.
  

Pthreads extension can achieve real multi-threading, I was the former Apple push service to be useful, but there are many pits.
Time-consuming tasks can be considered asynchronous and thrown into the message queue for processing.

Do not know the landlord's HTTP server with what, how can there be this strange "queue effect"?
Does the landlord limit the number of concurrent requests for the HTTP server, allowing only one request at a time?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.