fcntl.flock - how to implement a timeout?

前端 未结 4 636
离开以前
离开以前 2021-02-02 18:31

I am using python 2.7

I want to create a wrapper function around fcntl.flock() that will timeout after a set interval:

wrapper_function(timeout):
         


        
4条回答
  •  囚心锁ツ
    2021-02-02 18:43

    I'm sure there are several ways, but how about using a non-blocking lock? After some n attempts, give up and exit?

    To use non-blocking lock, include the fcntl.LOCK_NB flag, as in:

    fcntl.flock(self.__lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
    

提交回复
热议问题