I used the timer signal.setitimer, to exit the program after 5s, but failed
this is my code:
from signal import setitimer, signal, SIGALRM, ITIMER_REAL, SIGKILL
import os
if __name__ == "__main__":
def _exit(*args):
print("Timeout & Exit!!!")
os.kill(os.getpid(), SIGKILL)
signal(SIGALRM, _exit)
setitimer(ITIMER_REAL, 5)
os.system("ping 127.0.0.1")
setitimer(ITIMER_REAL, 0)
this is the result of running:
PING 127.0.0.1 (127.0.0.1) 56 (84) bytes of data.
64 bytes from 127.0.1: icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from 127.0.1: icmp_seq=2 ttl=64 time=0.017 ms
64 bytes from 127.0.1: icmp_seq=3 ttl=64 time=0.011 ms
64 bytes from 127.0.1: icmp_seq=4 ttl=64 time=0.011 ms
64 bytes from 127.0.1: icmp_seq=5 Ttl=64 time=0.011 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.011 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.011 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.010 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.012 ms
64 bytes from 127.0.1: icmp_seq=10 ttl=64 time=0.011 ms
64 bytes from 127.0.1: icmp_seq=11 ttl=64 time=0.011 ms
64 bytes from 127.0.0 .1: icmp_seq=12 ttl=64 time=0.011 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.011 ms
if I replace os.system ("ping 127.0.0.1") with a while True:pass, program that automatically exits after 5 seconds, who can help explain it?