登录
首页 >  Golang >  Go问答

共享库中的信号处理程序影响了 python 的 input() 中的“Ctrl + C”功能

来源:stackoverflow

时间:2024-02-08 10:45:23 232浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《共享库中的信号处理程序影响了 python 的 input() 中的“Ctrl + C”功能》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

当加载使用信号处理程序的共享库时,ctrl + c 在 python 中的 input() 或 getpass() 中不起作用。

如果不保存信号处理程序并稍后在 python 中恢复它们,有没有办法通过更改 go 代码来解决问题?

重现步骤:

  1. 转到某个目录。保存main.gomain.py文件(内容如下)。
  2. 运行以下命令生成 .so 文件(在 linux 或 mac 中)。 go build -o main.so -buildmode=c-shared main.go
  3. 运行 python3 main.py。如果按 ctrl + c,go 代码将被中断。
  4. 但是 ctrl+c 不会中断 input() 或 getpass() 方法。 (ctrl +d 或 ctrl+c 后按 enter 即可)。
  5. 要修复此问题,请在加载动态库之前保存原始处理程序,并在调用完成后恢复它们。

我们在 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)

以上就是《共享库中的信号处理程序影响了 python 的 input() 中的“Ctrl + C”功能》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>