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

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!

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.

This should get you started:

1 Like

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.

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

As I was saying, not my day...

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

The moment I read your comment I facepalmed so hard.

Thanks @Kevin.Herron

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

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

# Critical code

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

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.