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.
pturmel
September 24, 2024, 3:09pm
3
This should get you started:
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…
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.
pturmel
September 24, 2024, 3:13pm
5
This topic and its linked topics have additional guidance and examples:
Hello,
I'm seeking insights on backing storage and synchronization primitives for data accessed via the system-util-getGlobals function . 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…
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
import threading
system.util.getGlobals()["key"] = threading.Lock()
# Lock acquiring
system.util.getGlobals()["key"].acquire()
# Critical code
# Lock releasing
system.util.getGlobals()["key"].release()
pturmel
September 24, 2024, 3:58pm
7
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.