multithread implementation in PHP

Source: Internet
Author: User
Tags date implement log php code thread
Multithreading How to implement multithreading in PHP? See this headline, you must think I'm crazy. But in fact, I really did.

Here are some of my practices that have been tested. Yes, you can.

We know that PHP itself does not support multithreading, but our Web server supports multithreading.

That is, it can be accessed by more than one person at a time. This is also my PHP in the implementation of multithreading in the foundation.

Let's say we're running a.php this file. But I also requested the Web server to run another b.php in the program

Then these two files will be executed concurrently.

(PS: After a link request is sent, the Web server executes it, regardless of whether the client has exited)

Sometimes, we want to run not another file, but a part of the code in this file. What should we do?

In fact, it is through the parameters to control a.php to run which section of the program.

Let's look at an example:

a.php


PHP Code:--------------------------------------------------------------------------------

<?php
function Runthread ()
{
$fp = Fsockopen (' localhost ', $errno, $errmsg);

Fputs ($fp, "get/a.php?act=b\r\n\r\n"); The second parameter here is the request header specified in the HTTP protocol
I don't understand. Please see the definition in the RFC

Fclose ($FP);
}

function A ()
{
$fp = fopen (' Result_a.log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "\ r \ n");

Fclose ($FP);
}

Function B ()
{
$fp = fopen (' Result_b.log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "\ r \ n");

Fclose ($FP);
}

if (!isset ($_get[' act ')) $_get[' act ' = ' a ';

if ($_get[' act '] = = ' a ')
{
Runthread ();
A ();
}
else if ($_get[' act '] = = ' B ') b ();
?>

--------------------------------------------------------------------------------


Open Result_a.log and Result_b.log to compare the time of access in two files. As you can see, these two are actually running on different threads.
Some time is exactly the same.

The above is just a simple example that can be improved into other forms.


Since PHP can also be more than a thread, then the problem has come, that is the problem of synchronization. We know that PHP itself does not support multithreading. So there won't be anything like

The Synchronize method in Java. So how do we do that?

1. Try not to access the same resources. To avoid conflicts. However, you can operate as a database at the same time. Because the database is a concurrency-enabled operation. So in the multithreaded PHP

Do not write data to the same file. If you have to write, use a different method to sync. such as calling flock to lock the file. or create temporary files

And wait for this file to disappear while in another thread (file_exits (' xxx ')); This is equal to the presence of this temporary file, which means that the thread is actually operating

If this file is not there, other threads have released this.

2. Try not to read the data from the runthread after the execution of the socket fputs. Because to implement multithreading, you need to use non-blocking mode. That's like fgets this

Returns immediately when the function is a. So reading and writing data can be problematic. If you use blocking mode, the program is not multi-threaded. He's going to wait for the above return to execute.

The following procedure. So if you need to exchange data, you end up using outside files or data. If you really want it, use Socket_set_nonblock ($FP) to achieve it.


Having said so much, does this have any practical significance? When does this need to be used in this way?

The answer is yes. You know. In a continuous reading of network resources, the speed of the network is the bottleneck. If you take more of this form, you can simultaneously use multiple threads to

Different pages to read.

I do a can from 8848, Soaso these mall website search information program. There's also a program that reads business information and company catalogs from Alibaba's website

The technology. Because both programs are constantly linking their servers to read the information and save it to the database. The use of this technique eliminates the bottleneck in waiting for a response.


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.