0

I would like to run a project (project.py) that has two scheduling elements like this answer I found on stack overflow: Periodically execute function in thread in real time, every N seconds

One function is to run every x seconds the other every y seconds where 0.03 < x < 0.1 and 2 < y < 10.

function_x(t) calls an outside program (sync.py) that pauses (SIGSTOP) project.py

Question 0: After calling do_every, the program continues it's execution. Is this a correct assumption?

Question 1: If sync.py takes 2 seconds to execute before calling SIGCONT on project.py, will function_x's Timer be 'messed up'?

Question 3: If function_y is sending data over a socket, will function_x calling SIGSTOP mess up this execution? Will the timing be paused? How does the timer count time?

Answer referenced earlier:

import threading;

def do_every (interval, worker_func, iterations = 0):
  if iterations != 1:
    threading.Timer (
      interval,
      do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
    ).start ();

  worker_func ();

def print_hw ():
  print "hello world";

def print_so ():
  print "stackoverflow"


# call print_so every second, 5 times total
do_every (1, print_so, 5);

# call print_hw two times per second, forever
do_every (0.5, print_hw);
Community
  • 1
  • 1
channon
  • 362
  • 3
  • 16

1 Answers1

0

0) After calling do_every - it runs the worker_func once and then continues. yes. So your output will be:

stackoverflow
After first do_every
hello world
After second do_every
hello world
stackoverflow
....

1) I didn't really know if the timer would be messed up - so I ran a test using htop where I can send SIGCONT and SIGSTOP to any process - so, I ran do_every (2, print_so, 10); and after 5-6 "stackoverflow"s I stopped it. After a minute on starting it - it spewed out 4 more stackoverflows. So, no - the timer is not messed up.

3) It will not be messed up - what will happen is that if the socket has a timeout - and you pause for more than the timeout, the other side of the socket will assume your code is dead and will give a timeout exception. But if it's for less than the timeout, the socket will assume it was a temporary disconnection, and will continue streaming the data after that.

AbdealiLoKo
  • 3,261
  • 2
  • 20
  • 36