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您也可以想读 |



