A threaded script can use the following basic structure:
while( 1 ):
# do something here
# wait for a short time and then start the loop again
event.wait( 400 )
The script performs some actions, and then waits for a short time (400 milliseconds) before returning to the start of the loop. If the user redeploys the project, shuts down the node hosting the project, or changes the script via the Live Python device, it is during the 400ms time period that execution of the script pauses, and other processes running on the node can perform checks to see whether to halt the script altogether. If there is no reason to halt the script, the loop will continue running indefinitely.
Important Note: The event.wait statement relinquishes control to other processes to allow them to run. Without this statement, the Python code would consume all available CPU resources, until the node hosting the project stopped functioning correctly. At this point, the role would need to be stopped and then erased via the web interface (or front panel).
See also |