I have an application which is developed in PHP. In my app, I have hundreds of systems in record. Every 15 mins, I need to connect to all the systems to track their status (Like CPU usage), and return them back to my web page. The problem now is that the code connect to each system and execute the query command one by one, which makes the program REALLY slow. For now, it takes even more than 15 mins to track all the system status, which means that, at the second 15 min, it even has not finished the first time's query. Is there a way to connect to the systems in parallel in PHP? For example, ssh to the first 10 systems using one thread, and in the mean time, ssh to another 10 using another thread. Thanks.
Asked
Active
Viewed 295 times
0
-
a php script cannot perform threaded operations, but you can run several scripts at the same time. You could set a variable to only connect to X systems or set up separate scripts. or here is a good example: http://phplens.com/phpeverywhere/?q=node/view/254 – skrilled Aug 07 '13 at 19:47
-
If it is running from the CLI you could use PCNTL - http://www.php.net/manual/en/intro.pcntl.php – user1191247 Aug 07 '13 at 19:48
-
hope that helps: http://stackoverflow.com/questions/2585656/threads-in-php – Pedrão Aug 07 '13 at 19:52
1 Answers
0
Put the connection information (host names or IP addresses) of the systems in an array, then loop over them and fork for every system. After forking, execute your queries towards the system. Every query will happen in a new thread, running asynchronously with all the other queries. Wait for the forked children to finish their work, then exit the script.
This will work a lot faster than running every query synchronously since you don't have to wait for every response before moving on to the next query.
Depending on your server specs and the amount of remote systems, you might want to limit the amount of threads created.
Note: PHP pcntl is not available for windows (see here.)

Hubro
- 56,214
- 69
- 228
- 381
-
Thanks for help. The multiprocessing really works great for me! I think I don't need multi-threading in my case, since each query is independent here. – user1453951 Aug 07 '13 at 22:19