Skip to content

Commit 50d7c2b

Browse files
committed
minor #14366 [lock] Add documentation about serializing lock (jderusse)
This PR was merged into the 4.4 branch. Discussion ---------- [lock] Add documentation about serializing lock Commits ------- 81316ee Add documentation about serializing lock
2 parents 03057c0 + 81316ee commit 50d7c2b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

components/lock.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ method can be safely called repeatedly, even if the lock is already acquired.
7373
across several requests. To disable the automatic release behavior, set the
7474
third argument of the ``createLock()`` method to ``false``.
7575

76+
Serializing Locks
77+
------------------
78+
79+
The ``Key`` contains the state of the ``Lock`` and can be serialized. This
80+
allows the user to begin a long job in a process by acquiring the lock, and
81+
to continue the job in an other process within the same lock.::
82+
83+
use Symfony\Component\Lock\Key;
84+
use Symfony\Component\Lock\Lock;
85+
86+
$key = new Key('article.'.$article->getId());
87+
$lock = new Lock($key, $this->store, 300, false);
88+
$lock->acquire(true);
89+
90+
$this->bus->dispatch(new RefreshTaxonomy($article, $key));
91+
92+
.. note::
93+
94+
Don't forget to disable the autoRelease to avoid releasing the lock when
95+
the destructor will be called.
96+
97+
All stores are not compatible with serialization and cross-process locking:
98+
For instance the kernel will automatically releases Semaphores acquires by the
99+
:ref:`SemaphoreStore <lock-store-semaphore>` store.
100+
Wen a lock is acquired with such store, and hte application try to serialize
101+
the key, and exception will be thrown
102+
76103
.. _lock-blocking-locks:
77104
78105
Blocking Locks

0 commit comments

Comments
 (0)