Skip to content

Instantly share code, notes, and snippets.

@pborreli
Created May 10, 2010 14:42
Show Gist options
  • Save pborreli/396110 to your computer and use it in GitHub Desktop.
Save pborreli/396110 to your computer and use it in GitHub Desktop.
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