# Perspective Project Script Persistance

**URL:** <https://forum.inductiveautomation.com/t/perspective-project-script-persistance/93103>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [July 11, 2024, 6:50pm UTC](https://forum.inductiveautomation.com/t/perspective-project-script-persistance/93103 "2024-07-11T18:50:33Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![csjohnson](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@csjohnson](https://forum.inductiveautomation.com/u/csjohnson)\
**Post date:** [July 12, 2024, 12:41pm UTC](https://forum.inductiveautomation.com/t/perspective-project-script-persistance/93103/7 "2024-07-12T12:41:53Z")

</div>

Thanks again, Phil.  
Updated code:

```python
scanners = {}

def newScanner(session):
    if session:
    	newScanner = {
    		'session': session,
    		'scannerNumber': session.custom.scannerNumber,
    		'gps': {
    			'lat': session.props.geolocation.data.latitude,
    			'lng': session.props.geolocation.data.longitude,
    			'tstamp': session.props.geolocation.data.timestamp
    		}
    		
    	}
        
        scanners.setDefault(session.custom.scannerNumber, newScanner)
        refreshScannerTable()
        refreshScannerGps()

def removeScanner(scannerNumber):
    if scannerNumber in scanners:
        del scanners[scannerNumber]
        refreshScannerTable()
        refreshScannerGps()

def getScanner(scannerNumber = None):
    if scannerNumber is None:
        return list(scanners.values())
    else:
        return scanners.get(scannerNumber, None)

def updateScannerGps(scannerNumber, lat, lng, tstamp):
    if scannerNumber in scanners:
        scanner = scanners[scannerNumber]
        scanner['gps']['lat'] = lat
        scanner['gps']['lng'] = lng
        scanner['gps']['tstamp'] = tstamp
        
        refreshScannerGps()

```

> [@csjohnson](#):
>
> I can save that to `getGlobals()` and reinitialize my `scanners` dictionary after restart.  
> Any update to a scanner or new scanners coming online, will update the `getGlobals()` variable.

The `.setDefault()` function is "thread safe" for adding a new scanner to the dict. What would be a "thread safe" method for removing scanners as their sessions shutdown/expire? As in my function `removeScanner()`.

What about a session needing to update it's respective scanner's data?  
Does the below hold true?

From: [Python objects and thread safe - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/python-objects-and-thread-safe/15368/2)

> [@Python objects and thread safe](https://forum.inductiveautomation.com/t/python-objects-and-thread-safe/15368/3):
>
> Jython’s datastructures which are dictionaries, lists and sets are thread safe. So this means that there will not be data corruption of the data structures but there can be race conditions.
> 
> More information about this topic at this link: [http://www.jython.org/jythonbook/en/1.0/Concurrency.html](http://www.jython.org/jythonbook/en/1.0/Concurrency.html)

---

_[View the full topic](https://forum.inductiveautomation.com/t/perspective-project-script-persistance/93103)._
