Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does python process a signal?

What is the workflow of processing a signal in python ? I set a signal handler, when the signal occur ,how does python invoke my function? Does the OS invoke it just like C program? If I am in a C extend of python ,is it interrupted immediately ?


Now it's clear to me how does python process handle a signal . When you set a signal by the signal module , the module will register a function signal_handler(see $src/Modules/signalmodule.c) ,which set your handler and flag it as 1(Handlers[sig_num].tripped = 1;) , then call Py_AddPendingCall to tell python interpreter. The python interpreter will invoke Py_MakePendingCalls to call PyErr_CheckSignals which calls your function in main loop(see $src/Python/ceval.c). communicate me if you want to talk about this : [email protected]

like image 910
renenglish Avatar asked Jun 21 '11 03:06

renenglish


People also ask

Can Python be used for signal processing?

One of the key advantages of Python is that packages can be used to extend the language to provide advanced capabilities such as array and matrix manipulation [5], image processing [12], digital signal processing [5], and visualization [7].

How do you increase signal in Python?

You can use the os. kill method. Since Python 2.7 it should work (did not test it myself) on both Unix and Windows, although it needs to be called with different parameters: import os, signal os.


1 Answers

If you set a Python code signal handler using the signal module the interpreter will only run it when it re-enters the byte-code interpreter. The handler is not run right away. It is placed in a queue when the signal occurs. If the code path is currently in C code, built-in or extension module, the handler is deferred until the C code returns control to the Python byte code interpreter. This can be a long time, and you can't really predict how long.

Most notably if you are using interactive mode with readline enabled your signal handler won't run until you give it some input to interpret. this is because the input code is in the readline library (C code) and doesn't return to the interpreter until it has a complete line.

like image 195
Keith Avatar answered Sep 20 '22 04:09

Keith