How does php5 implement multithreading (asynchronous execution of multiple functions )?

Source: Internet
Author: User
I have a website Baidu Post Bar cloud to grab the second floor codeby changwei. The function is probably to grab the second floor in Baidu Post Bar. Now there are more than two hundred users using this website, my original execution process is in [BUG] ◇ 15-07-26 ◆ [WATER]♀Paste] changed the cron code and finally replied normally _ bug. This post clearly stated. The main idea is to set the cron To be executed every minute. php, then cron. in php, a user executes the command once, and then increases the user ID in the config table by 1. In the next round, the cron will be grabbed from the second floor of my website Baidu Post Bar cloud by changwei The function is probably to grab the second floor in the Baidu Post Bar. Now there are more than two hundred users on this website. My original execution process is in [BUG] ◇ 15-07-26 ◆ [Water♀Paste: Correct the cron code and finally reply to the bug. This post is very clear. The main idea is to set the cron To be executed every minute. php, then cron. in php, a user executes the command once, and increases the user ID in the config table by 1. In the next round, cron extracts the user ID from the config table, and then executes the next user to keep repeating, the consequence of this operation is that a total of more than two hundred users means that all users can seize the second floor in about three hours (more than two hundred minutes.
What I want to do now is to be able to execute the second floor of 20 users in a round of cron at the same time, so that 20 functions will be executed in cron. php at the same time. How can this be done? Can php do this? I have heard of a pthreads class before, but the official documents of this class cannot be understood. What should I do? Reply content: Obviously, your concurrency is very suitable for pcntl_fork (that is, multi-process). It can be used quickly and quickly. Just forget crontab. Simply write an endless loop in multiple processes.

Of course, you can also use the gearman and other task queues, which is very powerful.

Of course, for this IO-intensive type, you can also use generator. As a PHP programmer, your orders have already exploded. The case you mentioned is not the multithreading capability of php, but the cron. php file called by the system's cron at different points of time in its own framework. Multithreading is the pcntl_fork () function.

I used this to create a sofa grabbing program, which is full of disadvantages. It is very simple to implement multithreading with the pthreads extension.
This is a blog post I wrote about simple use of this extension:
Http://blog.xyao.org/php/497.html
But now the new version of pthreads2.0 only supports php7 after reconstruction. It is not appropriate to write such a thing in PHP version 1.0. It is a bit difficult for PHP. You can try Swoole extension, Swoole: asynchronous, parallel, and distributed extensions of PHP It seems that it can be executed asynchronously. I have never used it.

It is recommended to use go for writing. go's routine is very suitable for this multi-task concurrent crawling job. At the same time, it is easy to grab 200 concurrent users. go will automatically schedule the running of goroutine. Php does not support multithreading, but it has a pcntl extension that can be used for multi-process control. You can use this extension for your needs. For more information about the usage, see the PHP manual. You do not need to use scheduled tasks. 1. fork
2. gearman. Concurrency tool. We recommend using this. There are several workernodejs or python instances.

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.