2

I want to write a program for my network course and i have a socket that listen to receive data if it listen and receive no data i should terminate the program, i use threading.Timer to act like timer and have a line like t = threading.Timer(5, end_data) in my function that listen for receive data but i cant terminate program in end_data that is:

def end_data():
    sys.exit()

can any one help me? i also test below code bud did not terminate running program in terminal :(

def end_data():
    try:
        sys.exit()
    except:
        print"exception"

i expect that when stop terminal print Tinas-MacBook-Pro:~ tina$

i'm listening to socket in function named receive not main and when elapse 5 second with no data receiving it will run end_data and seems never return to receive function that part of this function is like below

def receive():
   s2 = socket(AF_INET, SOCK_DGRAM)
   s2.bind(addr3_2) 
   global end_call
   global base_window
   write=open('pictur.jpg','wb')
   s = socket(AF_INET, SOCK_DGRAM) 
   s.bind(addr3)
   while(end_call==0):
      t = threading.Timer(5, end_data)
      t.start()       
      recv_data, addr = s.recvfrom(3500)#size ro hala badan check kon
      t.cancel()

first i decide to set global var end_call after 5 second but it didn't work because it never come back to receive function

some thing that is very interesting for me is if define data_end like:

def end_data():
    os._exit
    print "Hi"

Hi will print in output :O

sandra
  • 949
  • 3
  • 12
  • 25
  • See this for some useful information: http://stackoverflow.com/questions/905189/why-does-sys-exit-not-exit-when-called-inside-a-thread-in-python. You could try using `os._exit`, but that's generally thought to be a bit impolite (It doesn't allow anything to be cleaned up) – mgilson May 17 '13 at 15:20
  • The reason you get `"Hi"` printed in your final example is that you're not actually *calling* `os._exit` -- try replacing `os._exit` with `os._exit()`. – Edward Loper May 17 '13 at 15:43
  • i try it but get error: TypeError: _exit() takes exactly 1 argument (0 given) – sandra May 17 '13 at 15:47

3 Answers3

0

Maybe try a setup like this

run_program = True

def end_data() :
    global run_program
    run_program = False

t = threading.Timer(5, end_data)
t.start()

while run_program:
    #do stuff
    time.sleep(1)

t.join() #maybe not needed

Make sure you called t.start() :)

beiller
  • 3,105
  • 1
  • 11
  • 19
  • I think you need to make `run_program` "`global`" in `end_data`. – mgilson May 17 '13 at 15:26
  • i'm listening to socket in function named receive not main and when elapse 5 second with no data receiving it will run end_data and seems never return to receive finction – sandra May 17 '13 at 15:29
  • Another method is to take a timestamp, then loop while polling the connection for data and sleep a time. Each loop check if more than 5 seconds has elapsed and it if has, break from the loop. No need for threading. – beiller May 17 '13 at 15:32
0

To answer your second question, with try, except.

This is the expected behavior per the pydoc:

"Since exit() ultimately “only” raises an exception, it will only exit 
the process when called from the main thread, and the exception is not intercepted."

Example:

def end_data():
    try:
        sys.exit("error!")
    except SystemExit, e: 
        print "Exception caught: ", e.args


print "begin"
end_data()
print "end"

Output:

$ ./test.py
begin
Exception caught:  ('error!',)
end
chrsblck
  • 3,948
  • 2
  • 17
  • 20
0

What about using threading Events ?

import threading

event = threading.Event()

def signal_stop():
    try:
        # do what you have to do
    finally:
        event.set()


t = threading.Timer(5, signal_stop)

print 'start thread and wait for it'
t.start()
event.wait()
print 'done'
Tommaso Barbugli
  • 11,781
  • 2
  • 42
  • 41