Hi, What would be the simplest way to execute a script installed on the PLCnext in a cyclic way? Example, we have a python script in the PLC that reads variables through REST Data Interface and we want to cyclically execute that script. Is there a way to call it from the PLC programming (Through PLCnext Engineer)? Thanks Robin
Robin, You can create a loop, see the below example. delay = 100 #100ms try: print("Press Ctrl+C to stop the script.")
while 1:
Inputs = Pullloop()
#(logic here)
Postloop(Outputs)
time.wait(delay)
except KeyboardInterrupt:
sys.exit()You could call it from Engineer, but keep in mind HTTP requests are synchronous (meaning they will not execute the next line of code until it has completed), and the time between pulls would not be as consistent as doing it in the program.
Hi Robin, In the next release of PLCnext Base library - due soon, I think - there will be a “Shell” function that you can call from PLCnext Engineer, which you could use to execute your Python script. Then, you could use an ESM cyclic task to implement the timed calling cycle. This shell command will be executed asynchronously, so it will not hold up the processing of the real-time task. ~ Martin.
Hi Martin, Was the "shell"function you mentioned ever released? I am also looking to execute a python scrip from PLCnext on a timed basis
![]()
Yes, the Linux Shell function block is now included in the PLCnextBase library, in the PLCnext Store. ~ Martin.
Have a look at threading.Timer. It runs your function in a new thread without using sleep(). from threading import Timer def hello(): print "hello, world" t = Timer(30.0, hello) t.start() # after 30 seconds, "hello, world" will be printed
Python sleep() Python sleep() will pause for an hour, day or whatever if given the proper value. It does not allow other processes take place (in same script) however. A better way is to use an event which will create an event on timeout.

