# Create a background script

**URL:** <https://forum.inductiveautomation.com/t/create-a-background-script/38400>\
**Category:** Ignition\
**Created:** [September 4, 2020, 1:52pm UTC](https://forum.inductiveautomation.com/t/create-a-background-script/38400 "2020-09-04T13:52:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patricia.mendez](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@patricia.mendez](https://forum.inductiveautomation.com/u/patricia.mendez)\
**Post date:** [September 4, 2020, 1:52pm UTC](https://forum.inductiveautomation.com/t/create-a-background-script/38400/1 "2020-09-04T13:52:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbabcock](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rbabcock/32/13425_2.png) [@rbabcock](https://forum.inductiveautomation.com/u/rbabcock)\
**Post date:** [September 4, 2020, 2:51pm UTC](https://forum.inductiveautomation.com/t/create-a-background-script/38400/2 "2020-09-04T14:51:32Z")

</div>

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

---

<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:** [September 4, 2020, 3:01pm UTC](https://forum.inductiveautomation.com/t/create-a-background-script/38400/3 "2020-09-04T15:01:21Z")

</div>

Start here:

> [@Best place to run system.serial.readBytes() in gateway scope for unsolicited data](http://forum.inductiveautomation.com/t/best-place-to-run-system-serial-readbytes-in-gateway-scope-for-unsolicited-data/37248):
>
> Hi As system.serial.readBytes() block script until its timeout pass, what is best place and method to call this functions without causing gateway CPU usage increasing. I’d like a mechanism like interrupt when I receive a char in serial line and then I start to capture my packet. As my device send data unsolicited putting it in a gateway timer may cause I lost packet.

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

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [September 4, 2020, 4:00pm UTC](https://forum.inductiveautomation.com/t/create-a-background-script/38400/4 "2020-09-04T16:00:49Z")

</div>

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.

```auto
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()
```
