0

I have an error "segmentation violation" when I try to execute my code two times and I have no more idea to solve this problem... :/

   func (a *MyActivity) Eval(context activity.Context) (done bool, err error) {

    pinString := context.GetInput("input1").(string)
    // string to int
    pinInt, _ := strconv.Atoi(pinString)

    //defer python3.Py_Finalize()
    python3.Py_Finalize()
    python3.Py_Initialize()
    if !python3.Py_IsInitialized() {
        fmt.Println("Error initializing the python interpreter")
        os.Exit(1)
    }

    dir, err := filepath.Abs(filepath.Dir("./"))
    if err != nil {
        //log.Fatal(err)
        fmt.Println(err)
    }

    ret := python3.PyRun_SimpleString("import sys\nsys.path.append(\"" + dir + "\")")
    if ret != 0 {
        //log.Fatalf("error appending '%s' to python sys.path", dir)
        fmt.Println("error appending to python sys.path" + dir)
    }
    // Here is the problem \\
    oImport = python3.PyImport_ImportModule("detect") //ret val: new ref
    if !(oImport != nil && python3.PyErr_Occurred() == nil) {
        python3.PyErr_Print()
        //log.Fatal("failed to import module 'detect'")
        fmt.Println("failed to import module 'detect'")
    }
    // Here is the problem \\
     .
     .
     .
     .
}

I tried many things but I couldn't solve the problem and it's new for me to use the library go-python3

Thanks a lot for your Help :)

Wsaitama
  • 75
  • 2
  • 11
  • If you want, I also can post the python code, but it's very short I don't think it's the problem – Wsaitama Aug 17 '21 at 13:00
  • 1
    https://stackoverflow.com/q/7676314/1256452 (and https://stackoverflow.com/q/67533541/1256452 as well)—a variant of "you're holding it wrong", more or less – torek Aug 17 '21 at 13:20
  • Thanks man, I tried the function init() in Golang and I put inside my import and my Py_Initialize() and it works now ... :) – Wsaitama Aug 17 '21 at 14:48

1 Answers1

0

I solved the problem with the function init() in Golang before my function Eval. I put inside the init() function all the imports and the python3.Py_Finalize() and python3.Py_Initialize() and it worked for me :)

Wsaitama
  • 75
  • 2
  • 11
  • You should not call the finalize function before calling initialize; since you're doing this in an `init` function, you should not call finalize at all. (Ideally, you'd call finalize *once*, at the "right time", as your app exits. Unfortunately it is in general impossible to locate the "right time"—so you tend to be best off not calling it at all, which itself is also unfortunate: there are often knock-on effects of the lack of a proper shutdown). – torek Aug 18 '21 at 01:14