Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Dealing with mass import in doctrine/pdo using pcntl_fork

View import
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
<?php
 
class importTask extends sfTask
{
protected function execute($arguments = array(), $options = array())
{
$xml = simplexml_load_file('file');
$users = array();
foreach ($xml->users as $user)
{
$users[] = $user;
 
if (count($users) < 10) // number of xml "users" you wanna put in each fork wagon
{
continue;
}
 
$pid = pcntl_fork();
if ($pid == -1)
{
die('could not fork, you need pcntl extension');
}
elseif ($pid)
{
pcntl_wait($status);
pcntl_waitpid($pid, $stat);
 
// we finished to process last wagon, let's prepare a new one
$users = array();
}
else
{
try
{
$this->processForking($users);
posix_kill(getmypid(), 9);
} catch (Exception $e)
{
posix_kill(getmypid(), 9);
}
}
 
}
// last users without fork
$this->processForking($users);
 
}
 
protected function processForking($users)
{
foreach ($users as $userXml)
{
$userNew = new User();
$userNew->fromArray($userXml); // or whatever
$userNew->save();
$userNew->free(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.