How can PHP be implemented in parallel?

Source: Internet
Author: User
How can PHP be implemented in parallel? 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'); // 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 broadcasting 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?


Reply to discussion (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.

Try curl_multi.

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!

The start time of 1086 and 1087 is the same.
Are you planning to output these two contents together?


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!

Install a crontab tool in windows

The start time of 1086 and 1087 is the same.
Are you planning to output these two contents together?
Yes. They do not interfere with each other to input various preset!

I don't know what it means,

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

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 remarks: 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 ~

Well, it's a bit difficult to use CSDN's built-in text editing ~ Let's take a look ~

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 remarks: 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 ~

Ah! I have also read this swoole, but the code is not like PHP? Besides, an error is reported when I copy data to the code. No. this is!

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.