Skip to content

Commit 232eb8e

Browse files
drew-512Drew O'Meara
and
Drew O'Meara
authored
all: make sure Context.Close is called
Co-authored-by: Drew O'Meara <[email protected]>
1 parent 01cbebd commit 232eb8e

File tree

5 files changed

+12
-0
lines changed

5 files changed

+12
-0
lines changed

examples/embedding/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func runWithFile(pyFile string) error {
2727

2828
// See type Context interface and related docs
2929
ctx := py.NewContext(py.DefaultContextOpts())
30+
31+
// This drives modules being able to perform cleanup and release resources
32+
defer ctx.Close()
3033

3134
var err error
3235
if len(pyFile) == 0 {

examples/embedding/mylib.module.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func init() {
4747
"GO_VERSION": py.String(fmt.Sprintf("%s on %s %s", runtime.Version(), runtime.GOOS, runtime.GOARCH)),
4848
"MYLIB_VERS": py.String("Vacation 1.0 by Fletch F. Fletcher"),
4949
},
50+
OnContextClosed: func(instance *py.Module) {
51+
fmt.Print("<<< host py.Context of py.Module instance closing >>>\n+++\n")
52+
},
5053
})
5154
}
5255

examples/embedding/testdata/embedding_out_golden.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Spring Break itinerary:
1515

1616
I bet Monaco will be the best!
1717

18+
<<< host py.Context of py.Module instance closing >>>
19+
+++

examples/multi-context/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ func RunMultiPi(numWorkers, numTimes int) time.Duration {
128128
w.ctx.RunCode(jobCode, w.main.Globals, w.main.Globals, nil)
129129
}
130130
workersRunning.Done()
131+
132+
// This drives modules being able to perform cleanup and release resources
133+
w.ctx.Close()
131134
}()
132135
}
133136

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func xmain(args []string) {
4848
opts := py.DefaultContextOpts()
4949
opts.SysArgs = args
5050
ctx := py.NewContext(opts)
51+
defer ctx.Close()
5152

5253
if *cpuprofile != "" {
5354
f, err := os.Create(*cpuprofile)

0 commit comments

Comments
 (0)