Skip to content

proper example completeness #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/embedding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions examples/embedding/mylib.module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
})
}

Expand Down
2 changes: 2 additions & 0 deletions examples/embedding/testdata/embedding_out_golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Spring Break itinerary:

I bet Monaco will be the best!

<<< host py.Context of py.Module instance closing >>>
+++
3 changes: 3 additions & 0 deletions examples/multi-context/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
}

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down