Home  >  Article  >  Backend Development  >  Thread pool - I want to use PHP to send emails and then record the sending results. Currently, I am using redis, which uses a script to perform sending and recording operations. It is very slow. Is there any better way?

Thread pool - I want to use PHP to send emails and then record the sending results. Currently, I am using redis, which uses a script to perform sending and recording operations. It is very slow. Is there any better way?

WBOY
WBOYOriginal
2016-09-27 14:18:11958browse

Currently, I am using a php script to execute a request to the external interface to send an email, then wait for the email to return, get the processing result, and then send the result to my own database record. .
It can process about 20,000 items in one hour, which is too slow. .

The boss said to use a thread pool to do it, but now I am confused again. . I don’t know the specific method. Can anyone explain the general process? Are you using the Thread extension?

Reply content:

Currently, I am using a php script to execute a request to the external interface to send an email, then wait for the email to return, get the processing result, and then send the result to my own database record. .
It can process about 20,000 items in one hour, which is too slow. .

The boss said to use a thread pool to do it, but now I am confused again. . I don't know the specific method. Can anyone explain the general process? Are you using the Thread extension?

I happened to do something similar and could give me some advice.
2w/h ≈ 5.55 QPS, which is indeed not high.

If according to your boss's idea, a single-threaded script is slow to execute, then use a few more. Then what you need is pthreads extension (multi-threading), or directly open multiple processes for processing (pcntl extension or even direct exec can be used).

But judging from your needs, the main thing that takes up processing time is external IO waiting. You can consider sending multiple emails concurrently (such as multi_curl), or sending emails asynchronously (such as curl's async), or using an asynchronous service as a whole (such as swoole extension)

Add a field process_id to the data table
When inserting database records, randomly assign process_id the value mt_rand(1,10)
Open 10 PHP scripts,
The first script processes the record with process_id 1: select * from send_email_list where process_id=1 and status=0
The second script processes records with process_id 2: select * from send_email_list where process_id=2 and status=0
In this way, 10 processes are processed at the same time. This is the simplest and most direct method, making your processing speed directly Improved by 10 times.

Multi-threading, multi_curl concurrency, and swoole asynchronous are all feasible solutions. However, the above solution only requires simple modifications on the existing basis, and may be more suitable for the subject.

Use nodejs to do background email service

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn