I write a web application in PHP with Symfony2. The user can upload a CSV file with data that is saved to the database. The parsing of each row of the CSV file last about 0.2 seconds because I make some requests to the Google Maps API.
So when you upload a CSV file with 5000 rows, which is a realistic case in my app, it may take 16 minutes to parse the whole file.
I don't want that the user must wait 16 minutes until he can continue using my app. So my question is: how can I parse the CSV file in the background, so that the user can continue surfing?
You can create a kernel.terminate
event listener and do your parsing there. This event fires after response is sent to the browser. Sample implementation would be,
Service declaration,
//services.yml
csv_parse_listener:
class: FQCN\Of\CsvParseListener
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }
Listener class,
namespace Your\namespace;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
class CsvParseListener
{
public function onKernelTerminate(PostResponseEvent $event)
{
$request = $event->getRequest();
if($request->get('_route') !== "Your_route"){
return;
}
$csvFile = $request->files->get('file_field_name');
//move file using $csvFile->move()
//read and parse
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With