![]() |
Signal Handling in Python
Signals are a limited form of inter-process communication, it analogous to hardware interrupts. It is generally used by the operating system to notify processes about certain issues/states/errors, like division by zero, etc. In case, you want to know read more about signals read Signal and Interrupts.
Python provides a set of functions in the signal which is used to handle signals. To do basic signal handling operations, we'll need to look into the signal.signal() method. The signal.signal() method needs to arguments, one is the signal number to handle and the second is the function that will be invoked when the signal is received. A working code can explain better than just words, so lets look into a few code samples to under signal handling in Python better. Simple: Print The Signal ReceivedIn the following code example I'll register handlers for SIGINT and SIGUSR1, which when received, the handler will print the arguments received. Code: Python
After executing the program, I'll send a signal to the process and we'll see the output. Code:
Timing Out A Blocking OperationAt times some functions/operation do not have a way to set a timeout for the operation to complete, as a result the program hangs indefinitely. Here, we'll utilize the SIGALRM to mimic a timeout, see below: Code: Python
|
Re: Signal Handling in Python
thank you brother, its really great and use full sharing, i really like it and i find it very use full, keep it up
|
| All times are GMT +5.5. The time now is 05:24. |