0

I'm trying to program two devices - the first by calling an application and manually clicking on program, and the second by calling a batch file and waiting for it to finish. I need each iteration of this loop to be 30 s so both devices can be programmed.

I've tried recording the time taken when it starts the iteration, and the time at the end of programming the second device. Then I set it to time.sleep(30-total time taken). This returns an execution time of slightly longer than 30 s per iteration.

for i in range(48):
  t1 = time.time()
  #program 1st board by calling app from python and clicking it using python.
  #wait a static number of seconds (s) as there is no feedback from this app.
  #program 2nd board by calling a batch file.
  #this gives feedback as the code does not move to the next line until the 
  #batch file is finished
  t2 = time.time()
  time.sleep(30-(t2-t1))
#some other code

Actual results: a little over 30 seconds. Expected results: exactly 30 seconds. Is this because of the scheduling in python?

  • How do you know that it is overtime, by human mind, don't ever depend on that, it isn't accurate. – U13-Forward May 15 '19 at 02:13
  • Im printing out the time taken per iteration, and I've excluded that code here. – inevitablenature May 15 '19 at 02:16
  • 1
    I'm no professional, but I assume that instructions take a couple of nanoseconds to execute the t1 and t2 assignment statements Additionally print statements are very expensive so they might also take some time. Also your first sentence isn't really clear... What kind of devices are you programming? Then in the comments you say "program 1st board"? What is a board? Why do you need to synchronize them? I don't think making both wait for 30 seconds is the right way to do it. – Ahmad Moussa May 15 '19 at 02:20
  • So I'm trying to automate the programming process of two chips on a PCB. Since we're programming multiple PCBs with different USB ports, we're also building a device to switch the ports. We need to declare a static delay while these are being programmed, then the hardware switches, and the next iteration takes place. – inevitablenature May 15 '19 at 02:24
  • 1
    A solution that does not involve static time delays will be much more robust. If you're building a device, then you should have all the control you need to respond to events ("programming finished") rather than relying on delays. – larsks May 15 '19 at 02:28
  • Take a look at this: https://stackoverflow.com/q/1557571/1896134 – JayRizzo May 15 '19 at 02:29
  • @larsks That is a good suggestion. I will look into it. It would involve significant restructuring of the firmware. – inevitablenature May 15 '19 at 02:30

1 Answers1

0

This is a result of scheduling in your operating system. When a process relinquishes the processor by calling sleep, there is no guarantee that it will wake up after the elapsed time requested in the call to sleep. Depending on how busy the system is, it could be delayed by a little, or it could be delayed by a lot.

If you have hard timing requirements, you need a realtime operating system.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • This confirmed my suspicions. Is there another way to limit the time from the operating system? Perhaps in powershell or a batch file? – inevitablenature May 15 '19 at 02:25