PHP fopen dosya kilitleme (durumun okuyucu / yazıcı tipi)

0 Cevap php

Bir PHP işlemi 3 kez hakkında bir saniye bir dosya yazma, ve sonra birkaç PHP işlemler bu dosyayı okurken nerede bir senaryo var.

Bu dosya esentially bir önbellek. Bizim web sitesi sürekli değişen verilere için çok ısrarlı yoklama vardır ve biz her ziyaretçinin DB onlar yoklar her zaman vurmak istemiyorum, bu yüzden saniyede DB 3 kez okur verilerini işleyen bir cron süreci var ve yoklama müşterilerine daha sonra okuyabilirsiniz bir dosyaya döker.

The problem I'm having is that, sometimes, opening the file to write to it takes a long time, sometimes even up to 2-3 seconds. I'm assuming that this happens because it's being locked by reads (or by something), but I don't have any conclusive way of proving that, plus, according to what I understand from the documentation, PHP shouldn't be locking anything. This happens every 2-5 minutes, so it's pretty common.

In the code, I'm not doing any kind of locking, and I pretty much don't care if that file's information gets corrupted, if a read fails, or if data changes in the middle of a read. I do care, however, if writing to it takes 2 seconds, esentially, because the process that has to happen thrice a second now skipped several beats.

Bu kod ile dosyayı yazıyorum:

$handle = fopen(DIR_PUBLIC . 'filename.txt', "w");
fwrite($handle, $data);
fclose($handle);

Ve ben doğrudan okuyorum:

file_get_contents('filename.txt')

(Statik dosya olarak müşterilerine doğrudan hizmet almak değil, ben dosyayı okur ve onunla bazı temel şeyler yapan bir normal PHP istek alıyorum)

Dosya 11KB hakkında, bu yüzden okuma / yazma için çok fazla zaman almaz. Peki 1ms altında.

Sorun olduğunda bu tipik bir günlük girdisi olduğunu:

  Open File:    2657.27 ms
  Write:    0.05984 ms
  Close:    0.03886 ms

Emin alakalı, ama apache üzerinden normal web istekleri olur okur, ama yazma Linux'un cron tarafından yapılan düzenli bir "komut satırı" PHP yürütme, Apache geçiyor değil değilse.

Any ideas of what could be causing this big delay in opening the file?
Any pointers on where I could look to help me pinpoint the actual cause?

Alternatif olarak, bu önlemek için yapabileceğim bir şey düşünebilirsiniz? Örneğin, ben fopen bir 50ms zaman aşımını ayarlamak edebilmek isterdim, ve bu dosyayı açmak olmasaydı, sadece önde atlar ve cron'nun sonraki çalışma bunun dikkat çekmenizi sağlar.

Yine, benim önceliğim, üç kez bir saniye dayak cron tutmak için her şeyin ikincil, yani herhangi bir fikir, öneri, bir şey son derece açığız.

Thank you!
Daniel

0 Cevap