In the provided code, the signal handler (sig_handler) is executed within the main thread context. The misconception may arise from the assumption that the signal handler runs in a separate thread. However, in Python, signal handlers are executed within the main thread, interrupting the execution flow at the point where the signal is received.
When a signal is received, the main thread temporarily suspends its current execution and invokes the signal handler. Once the signal handler completes its execution, the main thread resumes from where it left off. This is why you see the signal handler output (handler PID and current thread identity) within the same thread and process as the main execution.
It's important to note that signal handling in Python is not multi-threaded by default. If you want to handle signals in a separate thread, you would need to explicitly create a separate thread and dedicate it to signal handling using the signal.set_wakeup_fd() function.
In your provided code, since the signal handler is executing within the main thread, it has access to the same process ID (os.getpid()) and thread identity (threading.current_thread().ident) as the main execution.
In summary, the signal handler in your code runs within the same thread and process as the main execution, not in a separate thread.