Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2002
    Location
    Melbourne
    Posts
    217

    Cool handling multiple processes in PHP

    hi guys,

    i've managed to recomple php with pcntl under cpanel, which is great and i've got a question regarding multiple processes...

    what is the best way to prevent multiple processes from reading and getting your data file at the same time?

    obviously the case is that i hava a database with data in it and i have a php script that access that database, get some data that i want out of the db. I want to make this process quicker by creating few instances of the php script...

    the problem is that since those few instances of the php script (php processes) run almost at the same time, they are grabing the same data in my db and as the result, i'm getting duplicate data...

    ideally, if i have say 5 processes, the first one would go get few rows in db, the next process should get the next few available row instead...

    have anyone had experience/problem like this before?

    this is not limiting to just accessing the database though, it might be accessing the text file or even a mailbox to grab mails

    thanks for all the sugguestions, feedbacks guys

  2. #2
    so you're looking for several copies of the same php script to break up a problem into multiple parts, to speed things up? I would think first of all that the database side of things is probably your biggest bottleneck, so running one big query will do you better than running several smaller ones. Also, in order to get these simultaneous php threads to syncronize, you've got to have some way for them to talk to each other and agree on what to do next. This is going to put some serious overhead into the equation that you might not be expecting, unless you hard code each php process to do a specific part of the workload ahead of time.
    Phoenix Dedicated Servers -- IOFLOOD.com
    Email: sales [at] ioflood.com
    Skype: iofloodsales
    Backup Storage VPS -- 1TBVPS.com

  3. #3
    Join Date
    Mar 2002
    Location
    Melbourne
    Posts
    217
    well, it doesn't nessesary to be a big query, it might be small query that get the 5 latest row in db BUT instead of runing one instance of PHP script to do that, having 5 instances of the script runing...

    the challange here is indeed making the second process knowing that the first one is working so it has to wait, once the first one is done, then the second one can do the work and so on...

    i'm thinking of using a lock file, when first proces is run, it check the special hash file if it can aquire the lock, if it can, continue, if it can't, mean that other process has the lock, it goes to sleep...

    not sure that's the best way to do it or there is any other better ways

  4. #4
    i think you can do table locks in sql so it cant get ahold of it while the other is using it.

    before we go down this road of how to do what you're asking, maybe we should back up and say, what is it you're trying to accomplish that you think justifies this solution? you may find something else will work better, or at least, we can better understand what you're going for here.
    Phoenix Dedicated Servers -- IOFLOOD.com
    Email: sales [at] ioflood.com
    Skype: iofloodsales
    Backup Storage VPS -- 1TBVPS.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •