This is a discussion on "Multithreaded PHP" within the PHP Forum section. This forum, and the thread "Multithreaded PHP are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Multithreaded PHP
|
||
| Notices |
![]() |
|
|
LinkBack (55) | Thread Tools |
|
||||
|
Multithreaded PHP
Whilst working on a new webmaster tool (which I will unleash to you lot in the future) I ran into an issue...
The tool performs dozens of checks on a web page and then returns a report. The problem was that the speed was very very slow indeed and I needed a way to speed it up. What I needed was a way to handle these checks in a multithreaded way (or at least a pseudo multithreaded way) so I rang up the only person I knew who could offer a suggestion (Graham Ellis of Well House Consultants) He suggested a workaround and whilst it isnt true multithreading, it added severe speed to my application. Here are the two core files which make it all happen. This first file is the main php file you hit in your browser and for the sake of this example, needs a url passed in the querystring. The url is then passed to the 6 scripts by way of a command line parameter (yes, you read that right)
It executes each script by executing the php binary itself, so the processes themselves live outside of apache. The instant the scripts are fired, they work in the background whilst control is passed back to php to continue. After we've done other intensive processing (whatever code you insert above) we can finally get all the scripts return values. For this example, here is the code for one of the scripts that is called - note how it reads the url we passed from the command line
From my own tests, it's best to split up around 50% of the work. With that 50%, farm it out to a few different scripts and execute them with the above method. The other 50% can be processing within the main script before you finally read the results back in. There are probabally better ways to further optimise and maybe better ways to split up the work load, but all of that experimentation I shall leave up to you. Obviously this method can only be used in certain situations like mine... it wont make any differnce to scripts that are processor intensive, but for those which are not - this will reall add some speed if your script is suitable for splitting up in this way. Be sure to post here with any info you gather. Happy Coding Digg this article
__________________
Rob - SEO Specialist Owner & Founder of Webforumz.com I am currently unavailable for private work
Last Blog Entry: Creative Labs threaten developer over home made drivers.... (Apr 1st, 2008)
Last edited by Rob; Jan 4th, 2007 at 13:01. |
|
|
|
|||
|
Re: Multithreaded PHP
Many thanks for the "plug", Rob ... the work you've done and shown me is fascinating - I'll be coming back and running it against a sampling of my pages and it should be a great help to show us how we can improve our placement.
I have used similar techniques (though through Tcl/Expect or Python) in the past to check which systems are available on a network. It was a similar case to yours in that I wanted to check up on some 20 hosts, but couldn't afford to wait for serial ping timeouts. So I "paralleled" them. To this day, I have a Tcl page on our main web sites that, in a single click, tells me about some 10 different servers on which we look after sites ... and I can check from anywhere in the world. One caution for anyone using this technique. I believe that pipes have a 4k buffer in them, so your popen'd scripts could come to a temporary stop if they generate a lot of output and haven't been "reaped". |
|
||||
|
Re: Multithreaded PHP
That's cool! I can seriously use that in a project I'm working on!
|
|
||||
|
Re: Multithreaded PHP
If it works as you say it does, then I will also be able to use it in a current project. Thanks guys!
|
|
|||
|
Re: Multithreaded PHP
hi, i also needed some multithreading/parallel task.
i had a different approach, using WebService like interface and sockets. I have the main file, socket_cli_test.php, the one called by the user in the Browser. That files calls internally n other ones via socket connection (HTTP request) and then grabs the answers. Ive also implemented time out, because i needed the whole process to finish before 3 seconds. here is the code: socket_cli_test.php
hope it helps, ropu |
|
||||
|
Re: Multithreaded PHP
Thanks for sharing!
|
|
|||
|
Re: Multithreaded PHP
Hello
can anyone help me?? I am doing the code that Rob giving but there no output it only print out the name of the file. Is this code supported in window xp because i want to do php multithread. What i know that php multithread and forking is not supported in window platform and if supported it can only be used in CLI am i right. plzz someone help me i am stuck to do my project |
![]() |
| Tags |
| php, multithreading, multithreaded, multithread |
| Thread Tools | |
|
|