Thread pool-I want to use php to send emails and record the sending results. Currently, redis is used to execute sending and record operations with a script, which is very slow. Is there any better method?

Source: Internet
Author: User
Currently, a php script is used to execute an external interface request to send an email, and then wait for the email to return, get the processing result, and then send the result to your database record .. It's too slow to process about entries in an hour .. The boss said that the thread pool should be used for the job. now, I am overwhelmed .. Not familiar with this... currently, a php script is used to execute an external interface request to send an email, wait for the email to return, get the processing result, and then send the result to your database record ..
It's too slow to process about entries in an hour ..

The boss said that the thread pool should be used for the job. now, I am overwhelmed .. I don't quite understand the specific practices. can you tell me the rough process? Is Thread extension used?

Reply content:

Currently, a php script is used to execute an external interface request to send an email, and then wait for the email to return, get the processing result, and then send the result to your database record ..
It's too slow to process about entries in an hour ..

The boss said that the thread pool should be used for the job. now, I am overwhelmed .. I don't quite understand the specific practices. can you tell me the rough process? Is Thread extension used?

If you have done something similar, you can give some comments.
2 w/h ≈ 5.55 QPS, indeed not high.

If your boss thinks that the execution of a single-threaded script is slow, there will be a few more. What you need is the pthreads extension (multithreading), or you can directly start multiple processes for processing (pcntl extension or even directly exec ).

However, from the perspective of your needs, the main content that occupies the processing time is the external IO wait. You can consider sending multiple emails (such as multi_curl) concurrently, sending emails asynchronously (such as curl's async), or using an asynchronous service (such as swoole extension) as a whole)

Add a field process_id to the data table.
When inserting database records, the value of mt_rand () is assigned to process_id at random)
Enable 10 PHP scripts,
The first script processes records whose process_id is 1: select * from send_email_list where process_id = 1 and status = 0
The second script processes records whose process_id is 2: select * from send_email_list where process_id = 2 and status = 0
In this way, 10 processes are processed at the same time, which is the simplest and most direct way to increase the processing speed by 10 times.

Multithreading, multi_curl concurrency, and swoole asynchronization are all feasible solutions. However, the above scheme can be implemented only by making simple changes on the existing basis, which may be more suitable for the subject.

Use nodejs as the background email sending service

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.