PHP parallel implementation

Source: Internet
Author: User
How can PHP be implemented in parallel? This post is finally edited by hellon_no from 2014-02-2713: 51: 10 to implement a broadcast function. the broadcast table contains content, start time, end time, and interval. I use do & nbsp; while & nbsp; for scheduled processing. the code is as follows: how to implement do {$ ti & nbsp; strtotime PHP in parallel?

This post was last edited by hellon_no at 13:51:10

To implement a broadcast function, the broadcast table contains content, start time, end time, and interval.



The code is as follows:

Do {
$ Ti = strtotime (date ('Y-m-d H: I: s'); // The CURRENT TIMESTAMP

$ Array = $ this-> all_broadcast (); // broadcast array

Foreach ($ array as $ key => $ val ){
$ Intervals = 60; // (seconds) interval
$ Content = urlencode ($ val ['content']); // broadcast content

If ($ ti> = $ val ['starttime'] & $ ti <= $ val ['endtime']) {// Determine whether the current time is greater than or equal to the start time and less than or equal to the end time

$ Hello = $ this-> get_curl ($ content); // call the broadcast method to start
Broadcast sleep ($ intervals); // call once at intervals
}

Fclose ($ fp );
}

Sleep (10); // 10 seconds
} While (true );


Now the problem is: if broadcast is one, broadcast on time according to the interval. If there are several broadcasts, he cannot call the get_curl method N times at the same time. How can this be solved?
How can they play independently based on their start time, end time, and interval?
Does this require parallel implementation in PHP?
------ Solution --------------------
This problem seems to involve multithreading. if it can be done in java, you can run multiple threads.
Php does not support multithreading. However, you can use a benzene point to achieve your needs: scheduled tasks;
Crontab can be used in linux.
------ Solution --------------------
Try curl_multi.
------ Solution --------------------
The start time of 1086 and 1087 is the same.
Are you planning to output these two contents together?
------ Solution --------------------
Reference:
Quote: reference:

This problem seems to involve multithreading. if it can be done in java, you can run multiple threads.
Php does not support multithreading. However, you can use a benzene point to achieve your needs: scheduled tasks;
Crontab can be used in linux.
It's not a linux system. how can we implement it in win? Can you give me some suggestions!

------ Solution --------------------
Install a crontab tool in windows
------ Solution --------------------
Sleep (10); // 10 seconds
The content will not be displayed on the webpage. it will only be displayed after 10. it will be displayed at one time,
You seem to be an endless loop.

I am new too
------ Solution --------------------
Multithreading is not required for parallel processing ~ In addition, the php multi-threaded library knows what will happen under cgi. you must test it well and try it if you are interested.
Back to the topic, what the landlord may need is asynchronous ~
For example, when the program runs at a certain time, a notification is sent to a processing unit (whether implemented or not), and then the execution is executed directly without waiting for the processing result of the processing unit, thus saving the time ~
Laruence has discussed several implementation methods ~
Http://www.laruence.com/2008/04/14/318.html
In addition, the curl method is slightly changed. I added a comment.

I reviewed the document ~ It is found that curl has a millisecond-level timeout setting ~
CURLOPT_CONNECTTIMEOUT_MS
Note: Added in cURL 7.16.2. Available since PHP 5.2.3

CURL 7.24 and php5.4 ~ Dark cool ~


Of course, after such a long time, there may be other better methods ~ Http://www.swoole.com/is an example, private think this can be a large device, phper can no longer kneel to write code ~
------ Solution --------------------
Well, it's a bit difficult to use CSDN's built-in text editing ~ Let's take a look ~

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.