用户非阻塞锁，需要用户手动释放.

@param string $lock
@param int    $timeout

@return bool
506203
public static function unblock($lock, $timeout = 5) {
        try {
            $redis = RedisClient::getRedis();
            if ($redis) {
                $cnt = $redis->incr($lock);
                if ($cnt == 1) {
                    $redis->setTimeout($lock, $timeout);//设置超时，防止死锁

                    return true;
                }
            }
        } catch (\Exception $e) {
            log_warn($e->getMessage(), 'redis_lock');
        }

        return false;
    }
626455
protected function _timedOut()
    {
        $time = $this->read('Config.time');
        $result = false;

        $checkTime = $time !== null && $this->_lifetime > 0;
        if ($checkTime && (time() - (int)$time > $this->_lifetime)) {
            $result = true;
        }

        $this->write('Config.time', time());

        return $result;
    }
1065583
public function lock($key, $timeout = self::EXPIRE): bool
    {
        // $startTime = time();
        $file = sprintf('%s/%s.lock', $this->options['tmpDir'], md5(__FILE__ . $key));

        if (!$this->fp = fopen($file, 'wb+')) {
            throw new \RuntimeException('open file failed. FILE: ' . $file);
        }

        // LOCK_SH 取得共享锁定（读取的程序）
        // LOCK_EX 取得独占锁定,排它型锁定（写入的程序）
        // LOCK_UN 释放锁定（无论共享或独占）
        $op = $this->options['single'] ? LOCK_EX + LOCK_NB : LOCK_EX;

        // 如果锁定会堵塞的话（EWOULDBLOCK 错误码情况下），可选的第三个参数会被设置为 TRUE。（Windows 上不支持）
        return flock($this->fp, $op, $this->wouldBlock);
    }
468917
public function release_lock(lock $lock) {
        $params = array('locktype' => $this->dblockid,
                        'token' => $lock->get_key());
        $result = $this->db->get_record_sql('SELECT pg_advisory_unlock(:locktype, :token) AS unlocked', $params);
        $result = $result->unlocked === 't';
        if ($result) {
            unset($this->openlocks[$lock->get_key()]);
        }
        return $result;
    }
776539
public function execute($timeout = 10)
    {
        $req = array('operations' => $this->operations);

        $result = $this->MailChimp->post('batches', $req, $timeout);

        if ($result && isset($result['id'])) {
            $this->batch_id = $result['id'];
        }

        return $result;
    }
1265529
public function timeout($timeout = null, $connectTimeout = null)
    {
        if(null !== $timeout)
        {
            $this->timeout = $timeout;
        }
        if(null !== $connectTimeout)
        {
            $this->connectTimeout = $connectTimeout;
        }
        return $this;
    }
1356259
public function timeout($timeout = null)
    {
        if ($timeout !== null) {
            $this->lastTimeout = $timeout;
        }
        $this->ev->add($this->lastTimeout / 1e6);
    }
95341
public function brpop($keys, $timeout) {
        $keys = (array)$keys;
        return $this->returnCommand(['BRPOP'], $keys, [$keys, $timeout], ResponseParser::PARSE_ASSOC_ARRAY);
    }
821544
public function setTimeout($timeout)
    {
        $this->params['timeout'] = (int) $timeout;
        $this->buffer->setParam('timeout', (int) $timeout);

        return $this;
    }
270994
public function restore(string $data, int $timeout = 0): bool
    {
        return $this->handle->restore($this->name, $timeout, $data);
    }
636991
public function get_session_lock($rowid, $timeout) {
        parent::get_session_lock($rowid, $timeout);

        $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
        $sql = "SELECT GET_LOCK('$fullname', $timeout)";
        $this->query_start($sql, null, SQL_QUERY_AUX);
        $result = $this->mysqli->query($sql);
        $this->query_end($result);

        if ($result) {
            $arr = $result->fetch_assoc();
            $result->close();

            if (reset($arr) == 1) {
                return;
            } else {
                throw new dml_sessionwait_exception();
            }
        }
    }
227236
def lock(self, timeout=10):
        
        logger.debug("Locking %s", self.lock_file)
        if not os.path.exists(self.lock_file):
            self.ensure_path(self.lock_file)
            with open(self.lock_file, "w"):
                os.utime(self.lock_file)
        self._lock.acquire(timeout=timeout)
208499
public function iterate($timeout)
    {

        // call parent method and sleep for the default timeout
        parent::iterate($timeout);

        // create the instance and stack it
        $this->synchronized(function ($self) {

            // create the instance only if we're NOT dispatched and a class name is available
            if ($self->dispatched === false && $self->className) {
                // create the instance
                $instance = $self->getApplication()->search(ProviderInterface::IDENTIFIER)->get($self->className);

                // stack the instance
                $self->instances[] = $instance;

                // we're dispatched now
                $self->dispatched = true;
            }

        }, $this);
    }
294467
public function setTimeout($timeout = 60)
    {
        $this->timeout = $timeout;
        $this->debug->info(__FUNCTION__, 'setTimeout: ', $this->timeout);

        return $this;
    }
98044
public function get_session_lock($rowid, $timeout) {
        // NOTE: there is a potential locking problem for database running
        //       multiple instances of moodle, we could try to use pg_advisory_lock(int, int),
        //       luckily there is not a big chance that they would collide
        if (!$this->session_lock_supported()) {
            return;
        }

        parent::get_session_lock($rowid, $timeout);

        $timeoutmilli = $timeout * 1000;

        $sql = "SET statement_timeout TO $timeoutmilli";
        $this->query_start($sql, null, SQL_QUERY_AUX);
        $result = pg_query($this->pgsql, $sql);
        $this->query_end($result);

        if ($result) {
            pg_free_result($result);
        }

        $sql = "SELECT pg_advisory_lock($rowid)";
        $this->query_start($sql, null, SQL_QUERY_AUX);
        $start = time();
        $result = pg_query($this->pgsql, $sql);
        $end = time();
        try {
            $this->query_end($result);
        } catch (dml_exception $ex) {
            if ($end - $start >= $timeout) {
                throw new dml_sessionwait_exception();
            } else {
                throw $ex;
            }
        }

        if ($result) {
            pg_free_result($result);
        }

        $sql = "SET statement_timeout TO DEFAULT";
        $this->query_start($sql, null, SQL_QUERY_AUX);
        $result = pg_query($this->pgsql, $sql);
        $this->query_end($result);

        if ($result) {
            pg_free_result($result);
        }
    }
552230
public function set($key, $value, $timeout = 0)
    {
        if (is_int($timeout) && $timeout > 0) {
            $this->driver->set($key, (string)$value, $timeout);
        } else {
            $this->driver->set($key, (string)$value);
        }
    }
417415
public function timeout($timeout = null)
    {
        // Return value
        if (empty($timeout)) {
            return $this->timeout;
        }

        // Set value
        $this->timeout = $timeout;

        return $this;
    }