Author Topic: Detecting when a WindowsScript is Disposed  (Read 331 times)

Offline mbaker

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Detecting when a WindowsScript is Disposed
« on: July 06, 2016, 02:28:02 PM »
I'm working on a Python based WindowsScript which monitors a UDP Socket for data updating a SharedVariable based on the data that comes in. I have the core logic working fine but am running into two main issues.

1. Since the socket remains open indefinitely (listening for new data) my WindowsScript never really completes execution. Most of the time this is fine but when the channel changes or the player software exits I need to execute some cleanup logic to close the socket. Otherwise, the socket remains open and prevents future socket connections from listening to that port.

Is there some way to detect when the player is exiting or at least when the WindowsScript falls out of context so I can close the socket?

For reference, my Python Script looks somewhat like this:
Code: [Select]
# Monitors the current emergency state and notifies the
# host ScalaScript of any changes

import socket
import scala5

UDP_IP = ''
UDP_PORT = 5000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((UDP_IP, UDP_PORT))

while True:

data, addr = sock.recvfrom(1024)
print "received: "+data+" | from: "+str(addr)

except socket.timeout:

scala5.ScalaPlayer.LogExternalError(1000, "MonitorState", "There was an error receiving data from the server")

# Do something with the data here

2. Is there a way to only execute a WindowsScript once? Right now the WindowsScript is within a Sequence so it gets executed repeatedly. In my case, that means my script repeatedly tries to open new socket connections. One approach I could take is to define a SharedVariable called scriptInitialized that after the first execution would be set to true. That feels like a bit of a hack and I'm sure there's a better way.

Appologies if these are super straight forward questions (particularly #2). I'm still getting the hang of how the ScalaScripts work.


Offline mbaker

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: Detecting when a WindowsScript is Disposed
« Reply #1 on: July 08, 2016, 12:16:59 PM »
Alright, problems solved :)
Answers below in case this can help out someone else in the future.

#1 was a funny couple of bugs. I ended up reworking the loop a bit so that a new socket is opened at the start of each iteration and closed at the end. My script still wasn't being cleaned up but I eventually discovered two issues that were preventing cleanup.
 - After the scala player closed I was trying to call scala5.ScalaPlayer.Log("") before closing the socket. This threw an error preventing sock.close() from executing. So by trying to debug I was actually making the situation worse!
 - To help give the player a chance to quit I added a sleep between each loop iteration. I picked this tip up looking at the implementation of (

For #2, as I suspected, it was just my lack of understanding how the ScalaScript syntax worked. After reading through the documentation a bit more and reworking the script I created a situation where my WindowsScript only gets executed once.