(pseudo) Multiple threads and multiple processes in PHP

Source: Internet
Author: User

Learning (pseudo) Multiple threads and multiple processes in PHP

I have not looked into this information because I don't need it. There is a recent project that requires this kind of functionality.

Check out the PHP manual and other examples to learn the basics of two ways:

(pseudo) Multithreading: the use of external forces
Using the Web server itself multithreading to handle, from the Web server multiple calls we need to implement multithreaded program.
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
function Runthread ()
{
$fp = Fsockopen (' localhost ', $errno, $errmsg);
Fputs ($fp, "get/a.php?act=brnrn"); 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 (). "RN");
Fclose ($FP);
}

Function B ()
{
$fp = fopen (' Result_b.log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "RN");
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

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.