How To: Non-Blocking Semaphore Access in PHP Come: non bloccare l'accesso semaforo in PHP
Semaphore is used to restrict access to shared resources, such as shared memory or to limit the number of processes that may simultaneously use a resource, in a multi-process or multi-threaded environment. Semaforo è utilizzato per limitare l'accesso a risorse condivise, come ad esempio la memoria condivisa o di limitare il numero di processi che possono utilizzare simultaneamente una risorsa, in un processo multi-o ambiente multi-threaded.
In php using semaphore a process must wait while accessing a shared resource currently used by another process. In php semaforo utilizzando un processo deve attendere mentre l'accesso a una risorsa condivisa attualmente utilizzato da un altro processo. But this may not be required for all cases. Ma questo non può essere richiesto per tutti i casi. Sometimes we need to just verify whether a resource has been locked and move on. A volte dobbiamo solo verificare se una risorsa è stato bloccato e andare avanti. Unfortunately we have no way to check the status of a semaphore lock in php. Purtroppo non abbiamo alcun modo per verificare lo stato di un semaforo di blocco in PHP. Let’s see how we can implement non-blocking semaphore access in PHP. Vediamo come si può non attuare il blocco semaforo accesso in PHP.
As the semaphore library doesn’t give us what we need we will have to use file existence as a lock instead. Come il semaforo biblioteca non ci dà ciò di cui abbiamo bisogno dovremo utilizzare file di esistenza come un lucchetto. The concept and implementation is simple: Il concetto e la realizzazione è semplice:
1. Before accessing a shared resource you have to check whether a pre-determined file (say .lock ) exists and file modification time doesn’t exceed a fixed (configurable) expire time. Prima di accedere a una risorsa condivisa è necessario verificare se un pre-determinato file (dire. Lucchetto) esiste e file di aggiornamento tempo non superiori a un determinato (configurabile) scade il tempo. The second condition is required to recover from old locks which haven’t been cleared due to abrupt termination of the previous process or other reasons. La seconda condizione è necessaria per recuperare dal vecchio serrature che non sono state cancellate a causa della brusca cessazione del precedente processo o per altre ragioni.
2. If either of the condition is not satisfied in step 1 then create or modify the lock file and use the shared resource. In presenza di una delle condizione non è soddisfatta nel passaggio 1 quindi creare o modificare il file di lock e utilizzare la risorsa condivisa. After you are done, you must delete the lock file to allow subsequent access of the shared resource. Dopo aver finito, è necessario eliminare il file di lock per consentire il successivo accesso della risorsa condivisa. The deleting must be done irrespective of any error condition in earlier stages by proper exception / error handling of earlier steps to prevent abrupt termination. La eliminazione deve avvenire a prescindere da qualsiasi condizione di errore in precedenti fasi di buon eccezione / errore di manipolazione delle precedenti misure di protezione per impedire brusca cessazione.
3. If both the conditions are satisfied then the resource has been locked and not available currently. Se entrambe le condizioni sono soddisfatte, allora la risorsa è stato bloccato e non disponibili attualmente. Now you can either wait and check periodically in a loop with sleep or move on to next tasks. Ora è possibile attendere e verificare periodicamente in un ciclo con il sonno o passare alla prossima compiti.
Let look at how we can implement this with sample code: Consentitemi di esaminare il modo in cui siamo in grado di attuare con questo codice di esempio:
if(file_exists(".lock") && ((time() - filemtime(".lock")) < 1800)) { // The resource is locked.if (file_exists ( ". lock") & & ((time () - filemtime ( ". lock")) <1.800)) (/ / La risorsa è bloccata.You can either move on to next tasks or wait and check periodically in a loop } else { @touch(".lock"); // Use shared resource here // Ideally this should be done after error handling in previous steps, // so no errors caused above can prevent execution of this step.Potete passare alla prossima compiti o attendere e verificare periodicamente in un ciclo) else (@ contatto ( ". Lock"); / / Utilizzo risorsa condivisa qui / / Idealmente questo dovrebbe essere fatto dopo la gestione degli errori nella procedura precedente, / / così non errori causati sopra può impedire l'esecuzione di questo passo.@unlink(".lock"); // Release the lock }@ scollegare ( ". lock"); / / Rilascia il blocco)
Filed under Elencato sotto Headline News Headline News , PHP , Programming Programmazione | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | questo articolo |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |




