# How to make a lock using system.util.getGlobals()?

**URL:** <https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [September 24, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374 "2024-09-24T14:31:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [September 24, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/1 "2024-09-24T14:31:39Z")

</div>

Hi!

In one of the projects my team did they used this line of code to create locks:

`system.util.getGlobals()["key"].acquire()`  
`system.util.getGlobals()["key"].release()`

The ignition version of that project was `7.9.xx` and now we are using the `8.1.38` and I can't seem to get it to work.

Any help on how to do this? Or how to update it to 8.1?  
I'm missing something here... It can't be that hard.

Sorry, today is not my day.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 24, 2024, 2:56pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/2 "2024-09-24T14:56:20Z")

</div>

What doesn't work?

Also, FWIW, this code demonstrates acquiring and releasing the lock, but not creating it and putting it into the globals map in the first place.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:09pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/3 "2024-09-24T15:09:36Z")

</div>

This should get you started:

> [@Using system.util.getGlobals with classes](https://forum.inductiveautomation.com/t/using-system-util-getglobals-with-classes/78760/20):
>
> So, I was nerd-sniped pretty intensely by this yesterday, as the nesting of persistent dictionaries has some performance considerations that suggest some instance caching is appropriate. This is utterly untested, but should give you a framework to use if you decide you really don't want the latency of database storage (and I wouldn't use document tags): from threading import RLock \_g = system.util.getGlobals() # This class requires a dictionary in its constructor to # carry state. It delega…

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 24, 2024, 3:11pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/4 "2024-09-24T15:11:41Z")

</div>

I'm willing to bet that if you check in the logs, you'll find an error that looks like  
`AttributeError: ... object had no attribute 'acquire'`

It looks to me like you're expecting the object stored at `"key"` to have those methods. They don't. Check what objects are stored there in the projects that use that mechanism.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:13pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/5 "2024-09-24T15:13:38Z")

</div>

This topic and its linked topics have additional guidance and examples:

> [@Understanding Synchronization and Data Access with system.util.getGlobals](https://forum.inductiveautomation.com/t/understanding-synchronization-and-data-access-with-system-util-getglobals/86019):
>
> Hello, I'm seeking insights on backing storage and synchronization primitives for data accessed via the [system-util-getGlobals function](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-getGlobals#keywords). While it seems promising for my goals, I'm looking to better understand its underlying mechanisms to ensure correct implementation. Background I'm more experienced with Rust, so let me share a recent project as context. I had to manage mutable values maintained by their respective tasks running on threads within an asynchronous runtime, with a dedicated TUI t…

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [September 24, 2024, 3:52pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/6 "2024-09-24T15:52:28Z")

</div>

As I was saying, not my day...

The scrip that was suppose to initialize all the locks was not linked correctly 🙃  
So obviously, the lock never gets created and throws an exception.

The moment I read your comment I facepalmed so hard.

Thanks @Kevin.Herron

```python
import threading
system.util.getGlobals()["key"] = threading.Lock()

# Lock acquiring
system.util.getGlobals()["key"].acquire()

# Critical code

# Lock releasing
system.util.getGlobals()["key"].release()

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:58pm UTC](https://forum.inductiveautomation.com/t/how-to-make-a-lock-using-system-util-getglobals/96374/7 "2024-09-24T15:58:52Z")

</div>

> [@danielps1818](#):
>
> Correct implementation:

Definitely not. You should never initialize in globals with simple assignment, as that breaks anything already using the persistent lock object.

Go study the examples I linked. Really.
