#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0

    Parallel processing in PHP


    Hi,

    Would it be possible to send CLI commands to two (or more different servers (using phpseclib) in parallel and then collate the responses into a single output once all commands have been executed?

    Basically a php front end to a cluster of servers

    Thanks!!

    OS
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by O.S
    Hi,

    Would it be possible to send CLI commands to two (or more different servers (using phpseclib) in parallel and then collate the responses into a single output once all commands have been executed?

    Basically a php front end to a cluster of servers

    Thanks!!

    OS
    I've been dabbling in multi-threaded, asynchronous PHP programming which I know could do what you're after, but there might be less extreme ways.

    The only one that comes to mind, though, would be to fork your PHP process into multiple ones that initiate the connections, run the command(s), and outputs the responses to files you can then pick up with your main script and collate.

    Otherwise I think you'd be stuck doing it one at a time...
    LinkedIn: Dave Mittner
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Originally Posted by dmittner
    The only one that comes to mind, though, would be to fork your PHP process into multiple ones that initiate the connections, run the command(s), and outputs the responses to files you can then pick up with your main script and collate.
    Socket pairs (see the second example on that page). The master creates them to start with, then forks: the master uses socket #1 and closes #2 while the child uses #2 and closes #1. Then they can read and write to each other.
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by requinix
    Socket pairs (see the second example on that page). The master creates them to start with, then forks: the master uses socket #1 and closes #2 while the child uses #2 and closes #1. Then they can read and write to each other.
    Hmm. Interesting. And of course WAMP doesn't seem to support it and I guess it's unstable within a webserver, so I won't be able to play with it any until this evening.

    It looks like global scope is retained through the forking so each child process could return its response to a central collation function, so it seems it'd work.

    I'm not sure where socket pairs would fit in, though. It seems like you'd just create a child process for each server you need to connect to and open a normal socket per process.
    LinkedIn: Dave Mittner
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by dmittner
    Hmm. Interesting. And of course WAMP doesn't seem to support it and I guess it's unstable within a webserver, so I won't be able to play with it any until this evening.

    It looks like global scope is retained through the forking so each child process could return its response to a central collation function, so it seems it'd work.

    I'm not sure where socket pairs would fit in, though. It seems like you'd just create a child process for each server you need to connect to and open a normal socket per process.
    Thanks for all the responses. I have some stuff to google now and then try out. I'll post any progress. Might be back for further questions.
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Originally Posted by dmittner
    Hmm. Interesting. And of course WAMP doesn't seem to support it and I guess it's unstable within a webserver, so I won't be able to play with it any until this evening.
    Forking, yes. It depends on the threading model used by the web server. Short answer is you can't fork from a web server, only from a command-line script. However you can use the web script to run a CLI script...

    Originally Posted by dmittner
    It looks like global scope is retained through the forking so each child process could return its response to a central collation function, so it seems it'd work.
    Not retained but copied/shared. Children are basically identical to their parents immediately upon creation, and in fact the "only" way you can know in code whether you're the parent or child is by checking the return value from pcntl_fork().

    Originally Posted by dmittner
    I'm not sure where socket pairs would fit in, though. It seems like you'd just create a child process for each server you need to connect to and open a normal socket per process.
    The problem is communication between the parent and child. The parent wouldn't really need to say anything to the child, but the child (apparently) needs to send stuff back to the parent. That's where the socket comes in.
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    The problem is communication between the parent and child. The parent wouldn't really need to say anything to the child, but the child (apparently) needs to send stuff back to the parent. That's where the socket comes in.
    Ah, so that was the solution to pass the info back up to the parent. Wouldn't that just be shifting the problem, though? If there are multiple children then the parent would have to watch for the responses of multiple sockets at once (one from each child). Having to handle multiple sockets is what started this.
    LinkedIn: Dave Mittner
  14. #8
  15. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    socket_select

    As someone who's gone down this route before, I suggest using streams instead of straight sockets. Don't remember why but they are easier to work with.
    stream_socket_pair
    stream_select
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0
    In the case of phpseclib you could do $ssh->setTimeout(0.5) or something and then do $ssh->read(). So it'd return what it can after 0.5 seconds. You could then put that into a loop or something. eg. After 0.5 seconds get what it can from one SSH server then try another SSH server. After 0.5 seconds with that new SSH server move onto another etc.

IMN logo majestic logo threadwatch logo seochat tools logo