From 3ccfdff5467f0f28d0f8782b65e073c811d554ca Mon Sep 17 00:00:00 2001 From: Drew O'Meara Date: Sat, 12 Feb 2022 15:19:46 -0600 Subject: [PATCH 1/2] example completeness -- now calling ctx.Close() --- examples/embedding/main.go | 3 +++ examples/multi-context/main.go | 3 +++ main.go | 1 + 3 files changed, 7 insertions(+) diff --git a/examples/embedding/main.go b/examples/embedding/main.go index faa5120e..37f34a7f 100644 --- a/examples/embedding/main.go +++ b/examples/embedding/main.go @@ -27,6 +27,9 @@ func runWithFile(pyFile string) error { // See type Context interface and related docs ctx := py.NewContext(py.DefaultContextOpts()) + + // This drives modules being able to perform cleanup and release resources + defer ctx.Close() var err error if len(pyFile) == 0 { diff --git a/examples/multi-context/main.go b/examples/multi-context/main.go index 7559d60e..4bbeda8b 100644 --- a/examples/multi-context/main.go +++ b/examples/multi-context/main.go @@ -128,6 +128,9 @@ func RunMultiPi(numWorkers, numTimes int) time.Duration { w.ctx.RunCode(jobCode, w.main.Globals, w.main.Globals, nil) } workersRunning.Done() + + // This drives modules being able to perform cleanup and release resources + w.ctx.Close() }() } diff --git a/main.go b/main.go index 77fe4927..d529646a 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ func xmain(args []string) { opts := py.DefaultContextOpts() opts.SysArgs = args ctx := py.NewContext(opts) + defer ctx.Close() if *cpuprofile != "" { f, err := os.Create(*cpuprofile) From 9b3b8bed213193cf6b092c4fc77d0b84afee119a Mon Sep 17 00:00:00 2001 From: Drew O'Meara Date: Sat, 12 Feb 2022 15:20:44 -0600 Subject: [PATCH 2/2] now testing py.Context.Close() --- examples/embedding/mylib.module.go | 3 +++ examples/embedding/testdata/embedding_out_golden.txt | 2 ++ 2 files changed, 5 insertions(+) diff --git a/examples/embedding/mylib.module.go b/examples/embedding/mylib.module.go index 8a848411..4f2842b2 100644 --- a/examples/embedding/mylib.module.go +++ b/examples/embedding/mylib.module.go @@ -47,6 +47,9 @@ func init() { "GO_VERSION": py.String(fmt.Sprintf("%s on %s %s", runtime.Version(), runtime.GOOS, runtime.GOARCH)), "MYLIB_VERS": py.String("Vacation 1.0 by Fletch F. Fletcher"), }, + OnContextClosed: func(instance *py.Module) { + fmt.Print("<<< host py.Context of py.Module instance closing >>>\n+++\n") + }, }) } diff --git a/examples/embedding/testdata/embedding_out_golden.txt b/examples/embedding/testdata/embedding_out_golden.txt index c7443e07..445e0a65 100644 --- a/examples/embedding/testdata/embedding_out_golden.txt +++ b/examples/embedding/testdata/embedding_out_golden.txt @@ -15,3 +15,5 @@ Spring Break itinerary: I bet Monaco will be the best! +<<< host py.Context of py.Module instance closing >>> ++++