Hello everyone, how does PHP simulate multithreading?

Source: Internet
Author: User
Tags php multithreading
Hello everyone, how does PHP simulate multithreading Hello everyone? & nbsp; PHP & nbsp; how does PHP simulate multithreading for a long time. php code & nbsp;/*** Run * multithreading mechanism */function & nbsp; run ($ I) {// connect to the server: note that if you use an ssl/tls connection, the value of $ host is: "s Hello everyone, how does PHP simulate multithreading?
Hello everyone, how does PHP simulate multithreading?

It's been a long time, and it's not enough,

B. php code
/**
* Run
* Multithreading mechanism
*/
Function run ($ I ){
// Connect to the server: Note that if you are using an ssl/tls connection, the $ host value should be "ssl: // www.example.com" prefix.
// And $ port is 443
$ Host = $ _ SERVER ['http _ host'];
$ Port = 80;
$ Fp = fsockopen ($ host, $ port, $ errorno, $ errormsg); // connect to the server
If (! $ Fp ){
Die ("$ errorno ---------- $ errormsg ");
} Else {

$ I = "I = $ I"; // transmitted data
$ Path = '/a. php'; // request path
$ Method = 'post'; // request method (POST method)

// HTTP requests are generally GET or POST commands (POST is used for passing FORM parameters ). Format: method path/file name HTTP/1.1/r/n (protocol version)
$ Post = "$ method $ path HTTP/1.1/r/n ";
// GET: "GET/test/create_file.php? I HTTP/1.1/r/n ";

// The Host header specifies the Intenet Host and port number of the requested resource, which must represent the location of the original server or gateway of the request url. The HTTP/1.1 request must contain the host header domain; otherwise, the system returns the status code 400.
$ Post. = "HOST: {$ _ SERVER ['http _ host']}/r/n ";

// The Content-Type object header is used to indicate the media Type of the object to the receiver, specify the media Type of the request sent by the HEAD method to the receiver, or by the GET method.
$ Post. = "Content-type: application/x-www-form-urlencoded/r/n ";

// Content-length indicates the length (in bytes) of the HTTP body information ).
$ Post. = "Content-length:". strlen ($ I). "/r/n ";

$ Post. = "Connection: Close/r/n ";

$ Post. = "$ I/r/n"; // transmits data in POST mode. this parameter is not required in GET mode.
Fputs ($ fp, $ post );
}
Fclose ($ fp );
}

// Trigger multithreading
For ($ I = 0; $ I <= 2; $ I ++ ){
Run ($ I );
}



A. php
$ I = $ _ POST ['I']; // For a GET request: $ I = $ _ GET ['I']
File_put_contents ('d: s _ '. $ I.' _ '. time ().'. php', time ());
Php multithreading
------ Solution --------------------
Just learning php
------ Solution --------------------
Function conWrite ($ filename, $ content)
{
$ Filename_lock = $ filename. '. lock ';
While (1 ){
If (file_exists ($ filename_lock )){
Usleep (1000 );
} Else {
Touch ($ filename_lock); // lock
$ F = fopen ($ filename, 'w ');
Fwrite ($ f, $ content );
Fclose ($ f );
Unlink ($ filename_lock );
Break;
}
}
If (file_exists ($ filename_lock )){
Unlink ($ filename_lock );
}
}

------ Solution --------------------
Personal collection experience has always been a multi-thread crash... I feel PHP's support for multithreading is unfriendly.
------ Solution --------------------
Php itself does not support multithreading

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.