How to check if lock is acquired in Django pglock

45 views
Skip to first unread message

Sencer Hamarat

unread,
Sep 22, 2021, 10:01:46 AM9/22/21
to django...@googlegroups.com
Using django pglock
I want to check if lock is acquired by another process
How can I do it?


Sencer Hamarat

unread,
Sep 22, 2021, 11:23:02 AM9/22/21
to django...@googlegroups.com
Say, I have code block lie this:


           lock_id = "non_unique_lock"
with advisory_lock(lock_id, wait=True) as acquired:
# things to do


While one process has the lock, when the secondary process runs the same code block, the secondary process must raise an exception if it can't acquire the lock.
Please mind the lock id is the same for all processes.

Regards,
Sencer HAMARAT


Kasper Laudrup

unread,
Sep 22, 2021, 11:57:53 AM9/22/21
to django...@googlegroups.com
On 22/09/2021 17.18, Sencer Hamarat wrote:
> Say, I have code block lie this:
>
>
>            lock_id = "non_unique_lock"
>
> with advisory_lock(lock_id, wait=True) as acquired:
>
> # things to do
>
>
>
> While one process has the lock, when the secondary process runs the same
> code block, the secondary process must raise an exception if it can't
> acquire the lock.

Not that I've ever heard of django-pglocks before, but if I understand
the documentation at https://github.com/Xof/django-pglocks#usage
correctly, would it simply be a matter of setting wait to False and
raising an exception is acquired is False?

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Sencer Hamarat

unread,
Sep 23, 2021, 11:13:50 AM9/23/21
to django...@googlegroups.com
Thanks for reply, 

Afterwards, I figured out it was like in your comment.

Solution:

lock_id = "non_unique_lock"
with advisory_lock(lock_id, wait=False) as acquired:
    if acquired:
        # things to do
    else:
        raise Exception()  # or whatever you want.


Regards,
Sencer HAMARAT



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0d2443e8-ffad-aab7-d99e-97cc2ea86773%40stacktrace.dk.

Kasper Laudrup

unread,
Sep 23, 2021, 2:54:45 PM9/23/21
to django...@googlegroups.com
On 23/09/2021 17.12, Sencer Hamarat wrote:
> Thanks for reply,
>
> Afterwards, I figured out it was like in your comment.
>

Good to hear and thanks for sharing the solution.

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages