Update stock programaticly – Magento Stack Exchange

I have a custom import of stock from a CSV file, if I have a small number of products to update, it works fine.

But When I have 2400 (like I have here and now), the gateway times out.

The code looks like this, and I am executing it one item at the time.

$productToUpdate = $this->stockRegistry->getStockItemBySku($product->Ean);
$this->stockRegistry->updateStockItemBySku($product->Ean, $productToUpdate);

The last time I made something like this, I updated the database directly due to the speed issues, and then rebuilt the stock index.

But isn’t it possible to do it “the right way” and disable the update actions while updating, so I can run the updates and then the “post-update actions”?