Ctrl + C
当加载使用信号处理程序的共享库时,在 python 中的 input() 或 getpass() 中不起作用。
如果不保存信号处理程序并稍后在 python 中恢复它们,有没有办法通过更改 go 代码来解决问题?
重现步骤:
main.go
和 main.py
文件(内容如下)。.so
文件(Linux或Mac)。
go build -o main.so -buildmode=c-shared main.go
python3 main.py
。如果按 Ctrl + C,go 代码将被中断。我们在 Windows 中没有看到此问题。我们在 Linux 和 Mac 中都看到了这个问题。
main.go
package main
import (
"C"
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
//export addNums
func addNums(a int, b int) int {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// Handle SIGINT (Ctrl+C) and SIGTERM signals
osSignalChannel := make(chan os.Signal, 1)
signal.Notify(osSignalChannel, syscall.SIGINT, syscall.SIGTERM)
resultCh := make(chan int, 1)
defer func() {
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
signal.Stop(osSignalChannel)
close(osSignalChannel)
close(resultCh)
}()
go func() {
fmt.Println("Doing a long-running operation")
time.Sleep(5 * time.Minute)
resultCh <- a + b
}()
for {
select {
case <-ctx.Done():
fmt.Println("Context timeout reached")
return 0
case returnSignal := <-osSignalChannel:
fmt.Printf("Interrupted due to signal: %v\n", returnSignal)
signal.Stop(osSignalChannel)
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
return 0
case result := <-resultCh:
fmt.Println("Long-running operation finished successfully")
return result
}
}
}
func main() {
// This function is required to build a shared library, but it won't be used.
}
main.py
import ctypes
import getpass
import signal
import time
# Load the shared library
dll = ctypes.cdll.LoadLibrary('./main.so') # Path to the shared library
# Define the argument and return types of the function
dll.addNums.argtypes = [ctypes.c_int, ctypes.c_int]
dll.addNums.restype = ctypes.c_int
# Save the original signal handlers
# original_sigint_handler = signal.getsignal(signal.SIGINT)
# original_sigterm_handler = signal.getsignal(signal.SIGTERM)
# Call the C function
result = dll.addNums(5, 3)
# Restore the original signal handlers
# signal.signal(signal.SIGINT, original_sigint_handler)
# signal.signal(signal.SIGTERM, original_sigterm_handler)
print(f"Result of addNums(5, 3): {result}")
p = input("Press Ctrl + C to exit :) ")
# p = getpass.getpass("Press Ctrl + C to exit :) ")
print(p)
让库安装自己的信号处理程序会使管理变得更加困难。考虑让它公开一个处理函数,并在捕获时从 Python 调用它。
def handle_signal(signum, stack):
dll.handleSignal(signum)
signal.default_int_handler()
# ...
signal.signal(signal.SIGINT, handle_signal)
signal.signal(signal.SIGTERM, handle_signal)