From 8c8860ff960840e115ad666b981d8aa23e0bb984 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 10 Nov 2024 22:35:49 +0800 Subject: [PATCH] start subprocess in gradio demo --- README.md | 6 ++++-- demo/gradio/gradio.go | 6 ++++-- python.go | 9 +++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5b4fb76..09a39f5 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,7 @@ point.print() package main import ( + "fmt" "os" . "github.com/cpunion/go-python" @@ -273,8 +274,9 @@ func updateExamples(country string) Object { func main() { if len(os.Args) > 2 { - // avoid gradio start subprocesses - return + // start subprocesses + fmt.Println("start subprocess:", os.Args) + os.Exit(RunMain(os.Args)) } Initialize() diff --git a/demo/gradio/gradio.go b/demo/gradio/gradio.go index 14658cf..dfeefe0 100644 --- a/demo/gradio/gradio.go +++ b/demo/gradio/gradio.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" . "github.com/cpunion/go-python" @@ -42,8 +43,9 @@ func updateExamples(country string) Object { func main() { if len(os.Args) > 2 { - // avoid gradio start subprocesses - return + // start subprocesses + fmt.Println("start subprocess:", os.Args) + os.Exit(RunMain(os.Args)) } Initialize() diff --git a/python.go b/python.go index 3e55b3f..31ce49c 100644 --- a/python.go +++ b/python.go @@ -111,6 +111,15 @@ func RunString(code string) error { return nil } +func RunMain(args []string) int { + argc := len(args) + argv := make([]*C.char, argc+1) + for i, arg := range args { + argv[i] = AllocCStr(arg) + } + return int(C.Py_BytesMain(C.int(argc), &argv[0])) +} + // ---------------------------------------------------------------------------- func check(b bool, msg string) {