Hello. I make a function to import the contents of a .csv file into a MySQL database. Records of more than 40,000 plus must be checked for duplicates. No effort was able to stuff the work time in a reasonable 30 seconds. Decided to run in the background using an AJAX request. And other AJAX-ohms from time to time to check the status of implementation.
The first script cyclically rewrites the file in the database and writes the current value in the $ _SESSION ['import'] variable.
public function action_importcore() { $auth = Auth::instance(); if ($auth->logged_in()) { if (isset($_GET['file'])and(isset($_GET['group']))) { $filename = $_SERVER['DOCUMENT_ROOT'].'/bases/'.$_GET['file']; $group = ORM::factory('group', $_GET['group']); $_SESSION['fullimport'] = filesize($filename); $fp = fopen($filename, 'r'); $colum = fgetcsv($fp, 255, ';'); set_time_limit(0); while ($dt = fgetcsv($fp, 255, ';')) { $person = ORM::factory('person', array($colum[1] => $dt[1])); if ($person->loaded()) { if (!$person->has('group', $group->id)) $person->add('group', $group); } else { $person->set($colum[1], $dt[1])->set($colum[0], $dt[0])->save(); $person->add('group', ORM::factory('group', $group)); } $_SESSION['import'] = ftell($fp); } } else { $this->request->redirect('/'); } } else { $this->request->redirect('/'); } }
The second reads the value of $ _SESSION ['import'], considers the percentage and gives it to AJAX.
public function action_importping() { $auth = Auth::instance(); if ($auth->logged_in()) { if (isset($_SESSION['import'])and(isset($_SESSION['fullimport']))) { echo round($_SESSION['import']/$_SESSION['fullimport']*100); } else { echo 50; } } else { $this->request->redirect('/'); } }
The problem is that during the execution of the first script, the second one does not respond. No script at all! The web server (by the way, Apache 2.2.22 under Windows) gives pictures and static pages, but hangs when you request to any .php script. As soon as the first script exits, all the others can run.
Help to understand what is the reason?
$_SESSION['import']
variable. - Indifferent