I am working on a project to validate multiple email Ids using this php class -> php-smtp-email-validation. The code works fine when I input a maximum of 10 email Ids and it takes about 30-40 seconds to give the result. But I want to input thousands of email Ids and check them simultaneously rather than checking them one by one. Since fsockopen() in a single php script will execute one by one only,
so, I am here to ask if somehow I can execute multiple fsockopen() simultaneously and get the results faster? Or is there a way I can execute multiple php scripts asynchronously?
So, I am here to ask if somehow I can execute multiple fsockopen() simultaneously and get the results faster?
Your are looking for asynchronous, or non-blocking I/O:
In computer science, asynchronous I/O, or non-blocking I/O is a form of input/output processing that permits other processing to continue before the transmission has finished.
A stream in non-blocking mode will return early rather than wait (block), allowing you to read from or write too another stream, or even perform some other task while the equivalent blocking code would force you to wait.
fsockopen
is capable of returning a suitable stream, and will in the case of TCP: You can write code that performs asynchronous I/O in PHP without the help of any frameworks or extensions.
If you wanted to write the code yourself, the following manual pages will be helpful:
Various extensions expose better polling mechanisms than stream_select
.
It is advisable that you use one of the many frameworks that exist for performing asynchronous I/O. They will usually choose the best polling mechanisms available. Some of these attempt to make implementation easier for someone who is used to writing synchronous, blocking code. All of them are well tested, which allows you to get on with the task at hand rather than worrying about implementing the patterns you want to use.
A non-exhaustive list of such frameworks:
Or is there a way I can execute multiple php scripts asynchronously?
I think you mean in parallel.
Yes there are ways to execute PHP code in parallel, from executing many instances of the same script (multi-processing), all the way to using multi-threading.
Parallel concurrency simply doesn't fit the use case here.