When sending emails in batches, we need to obtain the sending results, so we need to wait for the request results.
Is there a way in php to request the interface asynchronously, then get the data, and then execute the callback function?
Now it is a synchronous request interface, and then wait, which is very slow. . And the server cpu will increase during execution. .
Please enlighten me
The key point now is not to wait for the return of the request, because it is very slow, and directly call a certain piece of code when there is a return. . The redis queue is already in use, and the script is executing
php installs multi-threading extension. You can achieve it by extending it yourself, and js is also implemented in a single thread.
You can try to use
reactphp
,workerman
,swoole
these libraries to perform asynchronous tasksThis can be achieved with the curl_multi that comes with php
It is strongly recommended that you use workererman. If you use fsockopen, this is only sent out asynchronously, but it is not returned! If you want asynchronous processing, use workererman. Of course, swoole can also be used, but this has C language , some error messages are from C. If you know C you can also use this. Getting started with workerman is extremely easy. The specific implementation logic is as follows:
When you need to be asynchronous, you directly call Workerman to execute your logic. This Workerman is executed synchronously. After
Workerman executes the logic, it calls the task task in an asynchronous manner. This is workerman's async. workerman returns directly.
When your task is completed, it will be passed to the workerman, and the workerman will execute your asynchronous logic.
Give you a backup framework with PHP asynchronous communication in it
github-php-awesome translated
Queue
1. Store the information that needs to be sent in Redis or other memory cache.
2.php provides an interface that requires access to the key token. This interface loops through the email data cached in redis and sends it.
3. There are solutions for using scheduled tasks in both windows and linux. Regularly go to http to request this php interface and complete the sending. It is equivalent to asynchronous.