我想运行一个项目(project.py),它有两个调度元素,比如我在堆栈溢出时找到的答案:Periodically execute function in thread in real time, every N seconds
一个功能是每x
秒运行另一个y
秒
其中0.03 < x < 0.1
和2 < y < 10
。
function_x(t)
调用暂停(SIGSTOP
)project.py
问题0:调用do_every后,程序继续执行。这是正确的假设吗?
问题1:如果在project.py上调用SIGCONT
之前sync.py需要2秒才能执行,function_x
的计时器是否会被搞砸了&#39;?
问题3:如果function_y
正在通过套接字发送数据,function_x
调用SIGSTOP
会导致执行错误吗?时间会暂停吗?计时器如何计算时间?
前面提到的答案:
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);
答案 0 :(得分: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.