Create a background script

Dear experts,

I need to create a python script that will basically subscribe to an external services provider. Afterwards, the values of these services have to be mapped to certain tags already created in advance. What is the best way of setting this subscription script in the system?. This script will collect the updated values so basically it needs to be always active and running in the background in somehow. Ideally, this script would update the tag values within the same code, therefore it makes no sense to me to set it to a tag change level.
It should retrieve data (This I know already how to do it), and map the new values to certain tags and if possible in the same script. Any hint to do this and have the script always running?

Thanks so much
Best Regards,
Patricia

Sounds like something I would use a Gateway Event script on a timer to do.

Start here:

Especially take note of the memory leak opportunities and persistent dictionary solutions.

I setup a file watcher background thread with system.util.invokeAsynchronous() and @pturmel 's system.util.persistent(). I invoke it in a gateway startup script, which updates another dictionary variable named status. I have a tag that polls the status, if it wasn’t manually closed and is not running I automatically restart it with tag change event.

persist = system.util.persistent('fileWatcher')
if 'fw' not in persist:
	persist['status'] = 'running'	
	persist['fw'] = FileWatcher()

def get():
	return persist['fw'], persist['status']	

def stop():
	persist['status'] = 'closed'	
	persist['fw'].kill()
	return get()
	
def restart():
	persist['status'] = 'running'
	persist['fw'].run()
	return get()
1 Like