Skip to content

Commit 232eb8e

Browse files
drew-512Drew O'Meara
andauthored
all: make sure Context.Close is called
Co-authored-by: Drew O'Meara <drew@plan-systems.org>
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy