0

I have a python code which start a UDP communication on a port 5002. So what happens is when I first run the code, it works normally then I close the python script using ctlr z/ctrl c and then again run the code it shows me below error:

sock.bind(address)
OSError: [Errno 98] Address already in use

This may be because the python script is running and keeping the port busy. So it shows me the above error. To resolve this, I thought of running these two commands right before the code kills sock.close() to close the socket and sys.exit() to exit the python script properly. Thats why I need to know how can I run these commands right before the code is killed. I cannot use KeyboardInterrupt exception because in future this will be running as a service.

I found this answer but not able to implement it. For example, if I do:

import atexit
import time

def exit_handler():
    print("STOPPED")

while True:
    print("Running....")
    time.sleep(1)
    atexit.register(exit_handler)

This keeps on running but when I kill the code it doesn't print Stopped. How to implement it or is there any alternative way of handling my situation.

Thanks

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • 1
    https://stackoverflow.com/a/1112350/5270506 – Farhan.K Nov 24 '17 at 10:44
  • https://stackoverflow.com/questions/4205317/capture-keyboardinterrupt-in-python-without-try-except – running.t Nov 24 '17 at 10:46
  • @Farhan.K I do not want to kill the code with keyboard interrupt. There will be a service which will start or stop this code. So cannot use keyboard interrupts – S Andrew Nov 24 '17 at 10:47
  • Ah okay, I thought you meant keyboard interrupts because you mentioned ctlr z/ctrl c in the question – Farhan.K Nov 24 '17 at 10:53
  • 1
    If you are getting "address already in use", it means that your script is still working (so you didn't kill it actually). If you actually killed it, it would free the resources and the port would be available. – Borys Serebrov Nov 24 '17 at 10:55
  • @BorisSerebrov Yes my script iss running thats why it is showing error. But I do not know why it keeps running when I am pressing `ctrl z/ctrl c` to stop it. – S Andrew Nov 24 '17 at 10:56
  • That's a different question, but if it's still running, then the exit handler will not work - script doesn't actually exit. Also I think `ctrl z` puts the script into backgroud instead of killing it. As for `ctrl c` there might be various reasons why it doesn't kill the script. – Borys Serebrov Nov 24 '17 at 10:59
  • If you really kill a process it can't do any cleanup. You must terminate it in a "nicer" way. You could e.g. open an additional control socket on which you send a command to terminate or, depending on OS, handle a SIGTERM (Un*x-like) or handle the request to stop the service on Windows. – Michael Butscher Nov 24 '17 at 11:00
  • Yep `ctrl z` sends to background. You can bring it to foreground using `fg` and then kill it using `ctrl c`. Or you can kill it after `ctrl z` using the `kill` command – Farhan.K Nov 24 '17 at 11:01

1 Answers1

1

The documentation of atexit module has an important note:

Note: The functions registered via this module are not called when the program is killed by a signal not handled by Python

That means that if you kill the script without special processing, the registered functions will not be called.

So you should instead register handlers for SIGTERM and SIGINT:

signal.signal(SIGTERM, (lambda signum, frame: exit_handler()))
signal.signal(SIGINT, (lambda signum, frame: exit_handler()))
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252