# Global objects in Perspective Scope

**URL:** <https://forum.inductiveautomation.com/t/global-objects-in-perspective-scope/53688>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [December 2, 2021, 5:19pm UTC](https://forum.inductiveautomation.com/t/global-objects-in-perspective-scope/53688 "2021-12-02T17:19:01Z")\
**Posts on this page:** 1\
**Showing post:** 13

<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:** [December 2, 2021, 8:24pm UTC](https://forum.inductiveautomation.com/t/global-objects-in-perspective-scope/53688/13 "2021-12-02T20:24:55Z")

</div>

Replace it unconditionally in the top level of the defining script. Something like this:

```auto
class myClass():
	__init__ (self, prior):
		# Copy important values from prior version, as long as they
		# are platform data types. Stop any threads stored in prior
		# and remove any of its listeners from the platform.
		pass

# Get the persistent global dictionary
_g = system.util.getGlobals()

# Protect the swap with a lock. Locks are defined by
# the jython stdlib, so do not have to be replaced on restarts.
# Use setdefault for assignment to ensure atomicity.
singletonLock = _g.get('singletonLock', None)
if singletonLock is None:
	import thread
	singletonLock = _g.setdefault('singletonLock', thread.allocate_lock())

# Swap old and new under the lock. This prevents
# races amongst multiple scripting contexts importing this script
# simultaneously. Just note that a new version might be replaced by
# another script context as soon as the lock is released.
with singletonLock:
	_g['singletonKey'] = myClass(_g.get('singletonKey', None))

# Use the following pattern in functions where you need to use the
# singleton. Whatever you do, never sleep or do long calculations
# while holding the lock.

# with singletonLock:
# c = _g[singletonKey]
# # Do something with c

```

---

_[View the full topic](https://forum.inductiveautomation.com/t/global-objects-in-perspective-scope/53688)._
