Multi-thread-PHP how to send requests in batches !!!!

Source: Internet
Author: User
For example, a web Background page requires an operation to send emails or push emails to tens of thousands of users ~ What should I do to transfer to php without affecting other operations after this operation? Because php requests are processed in a serial mode, you can call a function method to exit after it is run. If... for example, a web Background page requires an operation to send emails or push emails to tens of thousands of users ~ What should I do to transfer to php without affecting other operations after this operation? Because php requests are processed in a serial mode, you can call a function method to exit after it is run. If it is sent cyclically, at the time of sending, the system can only be stuck on this page and cannot perform other operations. In addition, we have found a java-like multi-thread implementation method ~ Call a method to send requests cyclically, and then the main process exits. Call an interface to check the sending status and the number of sent requests. The result is not acceptable because PHP can only process such web requests in parallel ~ This is different from the high-concurrency call used by php for app interfaces, because when php is used as this data request, it is called by different users ~ Apache supports parallel processing ~ So how to implement it?

Reply content:

For example, a web Background page requires an operation to send emails or push emails to tens of thousands of users ~ What should I do to transfer to php without affecting other operations after this operation? Because php requests are processed in a serial mode, you can call a function method to exit after it is run. If it is sent cyclically, at the time of sending, the system can only be stuck on this page and cannot perform other operations. In addition, we have found a java-like multi-thread implementation method ~ Call a method to send requests cyclically, and then the main process exits. Call an interface to check the sending status and the number of sent requests. The result is not acceptable because PHP can only process such web requests in parallel ~ This is different from the high-concurrency call used by php for app interfaces, because when php is used as this data request, it is called by different users ~ Apache supports parallel processing ~ So how to implement it?

This problem has been found a simple solution that does not need to reference the database.
Http://www.htmlgoodies.com/beyond/javascript/providing-feedback-on-long-running-scripts-in-older-browsers.html
Paste a URL.
The principle of the method is to use the session_write_close () function. When a long loop is running, add session_write_close () to the long loop to close the session ~ Because php web services are serialized for users of the same session, closing a session is equivalent to throwing a lock. Other services can queue up to get the lock execution program ~
In general, it is equivalent to throwing a lock every time in a long loop, and other operations can be inserted, so that the switching operation is realized ~

My current practice is to use the redis queue to put all the messages to be sent in it, and then ignore it.
There is a background sending process to process the data in the queue
1. If you need to resend the message, put the failed message in a backup queue. Before each cycle starts, the data in the backup queue is put in the sending queue.
2. the php process is not recommended to be resident. Therefore, you can set the lifecycle of a process to 1 min and then use cron to restart the process.

Connect to a message queue and put the tasks you want to process into the message queue. redis can be used for simple purposes, and beanstalkd and rabbitmq can be used for complex tasks.
If you insist on using PHP, write the CLI script to get the message from this message queue and process your time-consuming tasks after obtaining the message.
You can also use other technologies, such as python and java, to view the actual situation of your team and the technical stack.

PS: PHP multi-task solution. Use pthread extension to implement multithreading or pcntl extension to implement multi-process, but do not do this on the web end.

The Message Queue can be used for asynchronous processing. Your web Background only triggers notifications such as real emails, which will not block operations on your web Background, there are many solutions for message queues. Simply put, you can use redis to implement one, or there are similar solutions on the Internet.
When the queue processes the message sending action, you can send the message based on the importance of your business. For example, if I send the message once, whether the message is successful or not, you must send the message successfully, if the message must be sent successfully, you can write the failed message to another queue for processing or log record. This is close to the company's business.

Use swoole

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.