PHPackages                             utopia-php/lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. utopia-php/lock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

utopia-php/lock
===============

A simple lock library to coordinate access to shared resources across coroutines, processes and hosts

0.2.0(1mo ago)17.9k↑36.1%1MITPHPPHP &gt;=8.3CI passing

Since Apr 24Pushed 1mo agoCompare

[ Source](https://github.com/utopia-php/lock)[ Packagist](https://packagist.org/packages/utopia-php/lock)[ RSS](/packages/utopia-php-lock/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Utopia Lock
===========

[](#utopia-lock)

[![Build Status](https://github.com/utopia-php/lock/actions/workflows/tests.yml/badge.svg)](https://github.com/utopia-php/lock/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/15aa2f99a249e42b77551268e3fac89679e6f7bae93c68c03d80f5dd04f70edd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f75746f7069612d7068702f6c6f636b2e737667)](https://camo.githubusercontent.com/15aa2f99a249e42b77551268e3fac89679e6f7bae93c68c03d80f5dd04f70edd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f75746f7069612d7068702f6c6f636b2e737667)[![Discord](https://camo.githubusercontent.com/6e418910df1b6eb524c6cbd88dbaf5a5aa294316eeadcd963e11262a319f6321/68747470733a2f2f696d672e736869656c64732e696f2f646973636f72642f3536343136303733303834353135313234343f6c6162656c3d646973636f7264)](https://appwrite.io/discord)

Utopia Lock library is a simple and lite library for coordinating access to shared resources in PHP applications. This library provides a single interface backed by four lock primitives — mutex, semaphore, file and distributed — for serialising work across coroutines, processes, hosts and clusters. This library is maintained by the [Appwrite team](https://appwrite.io).

Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework.

Getting Started
---------------

[](#getting-started)

Install using composer:

```
composer require utopia-php/lock
```

System Requirements
-------------------

[](#system-requirements)

Utopia Lock requires PHP 8.3 or later. We recommend using the latest PHP version whenever possible.

The `Mutex` and `Semaphore` primitives require the [Swoole](https://github.com/swoole/swoole-src) extension (&gt;=6.0). The `Distributed` primitive requires the [Redis](https://github.com/phpredis/phpredis) extension.

Features
--------

[](#features)

### Supported Primitives

[](#supported-primitives)

PrimitiveScopeBackingUse when`Mutex`Single worker, coroutine-scoped`Swoole\Coroutine\Channel(1)`Serialising access to an in-memory resource per worker`Semaphore`Single worker, coroutine-scoped`Swoole\Coroutine\Channel($permits)`Capping concurrent access (e.g. outbound request pool)`File`Single host, cross-process`flock()`Cron guards, shared-filesystem coordination`Distributed`Cross-host, cluster-wideRedis `SET NX EX` + Lua releaseCoordinating workers across machinesUsage
-----

[](#usage)

### The Interface

[](#the-interface)

All primitives implement the same `Utopia\Lock\Lock` interface:

```
