In the end I did rewrite it to handle the import in a scheduled job, which turned out to be a lot better.