title: Can Cron jobs be used to simultate multi threading with PHP?
body: <p>I have a mysql database table filled with 1000+ records, lets say 5000 records. Each record has a <code>processed</code> boolean flag, default to <code>false (0)</code>. What I'd like to do is have a PHP script run on cron every minute. Its code would be something like this:</p>
  
  <pre><code>&lt;?php
  process();
  
  function process()
  {
     $sql = "SELECT id FROM items WHERE processed = '0' ORDER BY id ASC LIMIT 1";
     $result = $this-&gt;db-&gt;query($sql);
  
     if (! $result-&gt;has_rows())
       die;
  
     $id = $result-&gt;getSingle('id');
     processItem($id); //Will set processed to 1 after processing is done
     process();
  }
  ?&gt;
  </code></pre>
  
  <p>It should be pretty clear what the above code does, it gets the id for the next record which is un-processed, processes it, and then calls the <code>process()</code> function again which repeats this process until there are no more items to be processed, at which point the execution would stop.</p>
  
  <p>By putting this script on Cron to run every minute, I hope to have multiple instances of this script all working at processing the items simultaneously, so rather than processing one item at a time, 5-10+ items could be getting processed simultaneously.</p>
  
  <p>1) Is this going to work the way I'm planning it? Any suggestions for improvements / things to watch out for?</p>
  
  <p>2) Should I have the script set a counter for the number of running instances, so whenever a cron job starts, it checks the counter, if 50 (?) instances are running it would exit without processing. That might keep the server from crashing by having too many running processes using up too much memory? Any thoughts?</p>