Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Dealing with mass import in doctrine/pdo using pcntl_fork
<?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.