Last active
October 14, 2015 19:57
-
-
Save withinboredom/622db7b4c7996b3805c9 to your computer and use it in GitHub Desktop.
processes in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
while (count($standby) > 0) { | |
if (count($running) < $threads_per_second - 1) { | |
$item = array_shift($standby); | |
if ($item['attempts'] < $item_attempts) { | |
array_unshift($running, $item); | |
$spawned++; | |
echo "Processing " . ++$this->total_count . ": " . $running[0]['code'] . "\n"; | |
//spawn off a child | |
$running[0]['attempts']++; //increase the attempts | |
$running[0]['process'] = proc_open('exec nice -n 10 php -f ' . ROOT_DIR . '/trade/process/request_offers.php ' | |
. ' "'.$this->email_arg.'" ' | |
.' "'.$this->session_id_arg.'" ' | |
.' "'. $this->name_arg.'" ' | |
.' "'. $this->file_name_arg.'"' | |
. ' "'. $this->admin_arg .'" ' | |
. ' "'.$this->current_file.':'.$this->total_files.'" ' | |
.' "'.$this->bulk_flag.'" ' | |
.' "'.$running[0]['code'].'" ' | |
.' "'.$this->marketplace.'" ' | |
.DEBUG_LEVEL, | |
$process_description, $running[0]['pipes']); | |
} | |
else { | |
echo "UPC " . $item['code'] . " has utterly failed processing due to unknown reasons.\n"; | |
} | |
} | |
// check the status of all children | |
for($process = 0; $process < $spawned; $process++) { | |
$status = proc_get_status($running[$process]['process']); | |
//if we are no longer running | |
if (!$status['running']) { | |
//lower the spawn count as a process has died/exited | |
$spawned--; | |
// close the handle to the process and unset it | |
proc_close($running[$process]['process']); | |
unset($running[$process]['process']); | |
//get the status code -- if its not 2 then readd to the standby queue | |
if($status['exitcode'] != 2) { | |
array_push($standby, $running[$process]); | |
echo "UPC " . $running[$process]['code'] . " failed to resolve to an item - requeing item\n"; | |
} | |
else { | |
//echo "UPC " . $running[$process]['code'] . " completed successfully after " . $running[$process]['attempts'] . " attempts.\n"; | |
$this->update_statement->execute(array('count' => $completed_count++, 'total' => $total, 'id' => $this->session_id_arg)); | |
} | |
//regardless, release this usless mess into the world | |
unset($running[$process]); | |
} | |
} | |
// reindex the array | |
$running = array_values($running); | |
// wait 1/4 a second before looping | |
$wait = (int) (250000); | |
usleep($wait); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment