How To: Non-Blocking Semaphore Access in PHP 如何:非阻斷信號通道在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.信號燈是用來限制訪問共享資源,如共享內存或限制其進程數目可能同時使用一種資源,在一個多進程或多線程環境。
In php using semaphore a process must wait while accessing a shared resource currently used by another process.在PHP中使用信號燈的進程必須等待的同時訪問共享資源,目前所採用的另一個進程。 But this may not be required for all cases.不過,這可能不是所需的所有案件。 Sometimes we need to just verify whether a resource has been locked and move on.有時我們需要驗證是否只是一種資源已被鎖定,並繼續前進。 Unfortunately we have no way to check the status of a semaphore lock in php.不幸的是,我們沒有辦法檢查的地位,一個信號燈鎖定在PHP 。 Let's see how we can implement non-blocking semaphore access in PHP.讓我們看看我們如何能夠落實非阻斷信號通道在PHP 。
As the semaphore library doesn't give us what we need we will have to use file existence as a lock instead.由於信號燈圖書館沒有給我們什麼,我們需要我們將使用文件的存在,作為一個鎖。 The concept and implementation is simple:的概念和執行很簡單:
1. 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.之前,訪問共享資源,你必須檢查是否預先確定的文件(例如。鎖止)存在和文件修改時間不超過一個固定的(配置)屆滿的時間。 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.第二個條件是須收回舊鎖,其中並沒有被清除,由於突然終止前的過程中或其他原因。
2. 2 。 If either of the condition is not satisfied in step 1 then create or modify the lock file and use the shared resource.如果任何一方的條件是不是滿意,在第1步,然後創建或修改鎖定文件並使用共享資源。 After you are done, you must delete the lock file to allow subsequent access of the shared resource.之後,你是這樣做,您必須刪除鎖文件,讓隨後的訪問的共享資源。 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.該刪去必須做的,不論任何錯誤條件在較早階段進行適當的例外/錯誤處理以前的步驟,以防止突然終止。
3. 3 。 If both the conditions are satisfied then the resource has been locked and not available currently.如果雙方的條件感到滿意,那麼,資源已被鎖定,而不是目前可用的。 Now you can either wait and check periodically in a loop with sleep or move on to next tasks.現在您可以等待和檢查,定期在循環中與睡眠或提出對明年的任務。
Let look at how we can implement this with sample code:讓看看我們如何能夠落實這與示例代碼:
if(file_exists(".lock") && ((time() - filemtime(".lock")) < 1800)) { // The resource is locked.如果( file_exists ( “ 。鎖” ) & & ( (時間( ) -f ilemtime( “ 。鎖” ) ) < 1 800) ) ( / /資源是鎖定的。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.您可以提出對明年的任務或等待和檢查,定期在循環中)否則( @觸摸( “ 。鎖” ) ; / /使用的共享資源,在這裡/ /理想的,這應該做的事的錯誤處理後,在前面的步驟, / /因此,沒有任何錯誤所造成的上述可以防止執行這一步。@unlink(".lock"); // Release the lock }@斷開( “ 。鎖” ) ; / /釋放鎖)
Filed under提起下 Headline News頭條新聞 , , PHP PHP的 , , Programming編程 | |
| |
RSS 2.0 2.0 | |
Trackback Trackback跟踪 this Article |此文章|
Email this Article電子郵件此文章
You may also like to read您也可以想讀 |




































