PHPackages                             publero/critical-section - 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. publero/critical-section

ActiveLibrary

publero/critical-section
========================

Handles critical sections while executing multiple php scripts in parallel. Useful for cron and any other recurrently run scripts.

23.6k1PHP

Since Aug 26Pushed 12y ago3 watchersCompare

[ Source](https://github.com/Publero/critical-section)[ Packagist](https://packagist.org/packages/publero/critical-section)[ RSS](/packages/publero-critical-section/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

CriticalSection PHP library
===========================

[](#criticalsection-php-library)

[![Build Status](https://camo.githubusercontent.com/8c8b8943becabd0c4ab484273ca35d8efc6ffbb40b24e53153c0180866640b83/68747470733a2f2f7472617669732d63692e6f72672f5075626c65726f2f637269746963616c2d73656374696f6e2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/Publero/critical-section)

This library handles problem of code critical section which should be executed only once at a time.

Example use case
----------------

[](#example-use-case)

You have a cron task that is executing every minute, but it takes some time to execute. When server load's go critical it can take more than minute, which leads to bugs as a same cron is executed in multiple process at the same time.

You can encapsulate this code into critical section with use of this library.

```
$criticalSection = new FileCriticalSection();

$criticalSection->enter('cron_long_task');
doSomething();
$criticalSection->leave('cron_long_task');
```

This code example does take care of critical sections. You say which critical section should be entered. In this case it is `cron_long_task`. No other script can enter this critical section until the original one leaves the critical section. Keep in mind that critical section is left even if the script ends, or `CriticalSection` object is destructed.

If you want to enter critical section you can't script will wait until it can be entered. If you need to skip code which is already in critical section use timeout or `canEnter` method.

Timeout critical section
------------------------

[](#timeout-critical-section)

You can also do timeout enter. This way you can say explicitly say how long should the script wait to enter critical section. This is useful if you want to do something, but you know it only make sense in some time interval.

```
$criticalSection = new FileCriticalSection();

$code = __FILE__;
if ($criticalSection->enter($code, 30)) {
    doSomething();
    $criticalSection->leave($code);
}
```

Keep in mind that you have to check if enter returned true if you use timeout.

Check if code can enter critical section
----------------------------------------

[](#check-if-code-can-enter-critical-section)

If you are using everyminute cron (or even faster), we can assume that you can skip one or two calls here and there. To do so you can check if critical section can be entered before entering it.

```
$criticalSection = new FileCriticalSection();

$code = __FILE__;
if ($criticalSection->canEnter($code)) {
    $criticalSection->enter($code);
    doSomething();
    $criticalSection->leave($code);
}
```

Another way to do so is to enter critical section with zero timeout.

```
$criticalSection = new FileCriticalSection();

$code = __FILE__;
if ($criticalSection->enter($code, 0)) {
    doSomething();
    $criticalSection->leave($code);
}
```

License
-------

[](#license)

This bundle is under the MIT license. See the complete license in the bundle:

```
LICENSE

```

Reporting an issue or a feature request
---------------------------------------

[](#reporting-an-issue-or-a-feature-request)

Issues and feature requests are tracked in the [Github issue tracker](https://github.com/Publero/critical-section).

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/602eba7f96ee044b5e6ab1d8210f3c3037a868a359b7d90639d5259fd10b8da6?d=identicon)[publero](/maintainers/publero)

---

Top Contributors

[![mhlavac](https://avatars.githubusercontent.com/u/743421?v=4)](https://github.com/mhlavac "mhlavac (4 commits)")

### Embed Badge

![Health badge](/badges/publero-critical-section/health.svg)

```
[![Health](https://phpackages.com/badges/publero-critical-section/health.svg)](https://phpackages.com/packages/publero-critical-section)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
