8000 Merge branch '4.4' into 5.1 · symfony/symfony-docs@846baf5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 846baf5

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Add documentation about serializing lock
2 parents dcf0a15 + 61e30b3 commit 846baf5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

components/lock.rst

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

71+
Serializing Locks
72+
------------------
73+
74+
The ``Key`` contains the state of the ``Lock`` and can be serialized. This
75+
allows the user to begin a long job in a process by acquiring the lock, and
76+
continue the job in an other process using the same lock::
77+
78+
use Symfony\Component\Lock\Key;
79+
use Symfony\Component\Lock\Lock;
80+
81+
$key = new Key('article.'.$article->getId());
82+
$lock = new Lock($key, $this->store, 300, false);
83+
$lock->acquire(true);
84+
85+
$this->bus->dispatch(new RefreshTaxonomy($article, $key));
86+
87+
.. note::
88+
89+
Don't forget to disable the autoRelease to avoid releasing the lock when
90+
the destructor is called.
91+
92+
Not all stores are compatible with serialization and cross-process locking:
93+
for example, the kernel will automatically release semaphores acquired by the
94+
:ref:`SemaphoreStore <lock-store-semaphore>` store. If you use an incompatible
95+
store, an exception will be thrown when the application tries to serialize the key.
96+
7197
.. _lock-blocking-locks:
7298

7399
Blocking Locks

0 commit comments

Comments
 (0)
0