Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. Filesystem
  5. LockHandler
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • What is a Lock?
  • Usage

LockHandler

Edit this page

Warning: You are browsing the documentation for Symfony 3.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

LockHandler

What is a Lock?

File locking is a mechanism that restricts access to a computer file by allowing only one user or process access at any specific time. This mechanism was introduced a few decades ago for mainframes, but continues being useful for modern applications.

Symfony provides a LockHelper to help you use locks in your project.

Usage

Caution

The lock handler only works if you're using just one server. If you have several hosts, you must not use this helper.

A lock can be used, for example, to allow only one instance of a command to run.

1
2
3
4
5
6
7
8
use Symfony\Component\Filesystem\LockHandler;

$lockHandler = new LockHandler('hello.lock');
if (!$lockHandler->lock()) {
    // the resource "hello" is already locked by another process

    return 0;
}

The first argument of the constructor is a string that it will use as part of the name of the file used to create the lock on the local filesystem. A best practice for Symfony commands is to use the command name, such as acme:my-command. LockHandler sanitizes the contents of the string before creating the file, so you can pass any value for this argument.

Tip

The .lock extension is optional, but it's a common practice to include it. This will make it easier to find lock files on the filesystem. Moreover, to avoid name collisions, LockHandler also appends a hash to the name of the lock file.

By default, the lock will be created in the temporary directory, but you can optionally select the directory where locks are created by passing it as the second argument of the constructor.

The lock() method tries to acquire the lock. If the lock is acquired, the method returns true, false otherwise. If the lock method is called several times on the same instance it will always return true if the lock was acquired on the first call.

You can pass an optional blocking argument as the first argument to the lock() method, which defaults to false. If this is set to true, your PHP code will wait indefinitely until the lock is released by another process.

Caution

Be aware of the fact that the resource lock is automatically released as soon as PHP applies the garbage-collection process to the LockHandler object. This means that if you refactor the first example shown in this article as follows:

1
2
3
4
5
6
7
use Symfony\Component\Filesystem\LockHandler;

 if (!(new LockHandler('hello.lock'))->lock()) {
    // the resource "hello" is already locked by another process

    return 0;
}

Now the code won't work as expected because PHP's garbage collection mechanism removes the reference to the LockHandler object and thus, the lock is released just after it's been created.

Another alternative way to release the lock explicitly when needed is to use the release() method.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Measure & Improve Symfony Code Performance

Measure & Improve Symfony Code Performance

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of František Maša, a Symfony contributor

Thanks František Maša for being a Symfony contributor

2 commits • 66 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia