-
Notifications
You must be signed in to change notification settings - Fork 95
Multi-context execution (py.Context) #144
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
Closed
Closed
Changes from 65 commits
Commits
Show all changes
81 commits
Select commit
Hold shift + click to select a range
fc66b7f
py.CompileMode now a string enum
d10c35f
addressed benign Go warnings
2161b71
make Float implement M__index__
247ec42
added string helper functions
9a75271
updated to 1.15
90a8988
new Ctx-aware module model
c51644e
py.Ctx model allows concurrent execution
7f7d213
repl.New() now accepts an existing py.Ctx
708bb7e
fixed typo in callInternal()
a244256
unbroke test compatibilty
75d8743
cleaned up py.Ctx initialization wrt sys and DefaultCtxOpts()
d5fb8e8
added multi Ctx benchmark
269bdc0
WIP
4db9c65
fixed py.NewCtx not being set
53d05db
fixed List.sort() detection of instance invocation
7fd1c29
separated path resolution from path evaluation
8f3a104
removed cruft
5ebaf66
place ctx at top
d423190
err msg tweak
drew-512 b33c266
typo fix
drew-512 efca761
for loops we can all agree on
drew-512 1569475
removed outdated comments
a9c5f1f
Merge branch 'master' of https://github.com/drew-512/gpython
6372aee
added info for Frame.Ctx
4af4f90
removed unnecessary assignment
aec59f8
copyright and formatting
74f1a19
Ctx and NewCtx docs
8fa6622
switch cleanup
26fb9aa
leaner ModuleImpl schema
8614aeb
added GetDict() impl
86531c6
Compile() now returns py.Code (vs py.Object)
f71b961
tighter py.Ctx invocation patterns
77a7954
WIP
6bba7da
reverted to 644
46d1052
chmod 644
66fad23
enhanced RunFile() to enclose RunInNewModule()
95afd7b
Update examples/multi-ctx/main.go
drew-512 319edb7
Update modules/runtime.go
drew-512 06a2eae
Update modules/runtime.go
drew-512 8d4d586
Update modules/runtime.go
drew-512 d475e67
Update vm/eval.go
drew-512 6afac99
Update py/run.go
drew-512 931c346
Update modules/runtime.go
drew-512 a7ce4bb
Update modules/runtime.go
drew-512 c1f8809
Update modules/runtime.go
drew-512 9f02cf1
Update modules/runtime.go
drew-512 85e3a4a
Update modules/runtime.go
drew-512 fbec34e
Update modules/runtime.go
drew-512 dbd54d8
code cleanuo
71a5604
Module doc str
634823c
consistent arg order of NewFunction
6a0c11e
license and formatting
6242836
comment and cruft cleanup
61c7830
renamed Store to ModuleStore + docs
bb98aa8
added import docs
36b1018
shadowing cleanup
f2602bc
docs tweaks
e25e3bb
code cleanup
9421b6b
synced with gpython
92aed8a
Merge branch 'go-python:master' into master
drew-512 96515ea
Update modules/runtime.go
drew-512 f62f2b7
Update modules/runtime.go
drew-512 3bdba62
reverted from %w to %v (linux CI compile failure)
8033615
renamed py.Ctx to py.Context
681abf2
added Context.Close() and ModuleImpl.OnContextClosed()
7508556
docs and LoadIntsFromList
99bcb40
rename edits
0cc7650
push/popBusy now private
9afb2d3
doc edits
7d2a8d9
fixed kwarg issues in ParseTupleAndKeywords() and builtin_print test
ba7db80
added Flush kwarg to print test
61e42c8
added embedding example
168c337
main README makeover
2b7e1ab
fixed type conversion
3821b4b
Update README.md
drew-512 170f0d2
Update README.md
drew-512 b68b9f6
Update py/util.go
drew-512 759023b
Update py/util.go
drew-512 0c77716
Update py/util.go
drew-512 505755d
LoadIntsFromList cleanup
02d6b3e
comment edits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2022 The go-python Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"runtime" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
// This initializes gpython for runtime execution and is critical. | ||
// It defines forward-declared symbols and registers native built-in modules, such as sys and time. | ||
_ "github.com/go-python/gpython/modules" | ||
drew-512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// This is the primary import for gpython. | ||
// It contains all symbols needed to fully compile and run python. | ||
"github.com/go-python/gpython/py" | ||
) | ||
|
||
func main() { | ||
|
||
// The total job count implies a fixed amount of work. | ||
drew-512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The number of workers is how many py.Context (in concurrent goroutines) to pull jobs off the queue. | ||
// One worker does all the work serially while N number of workers will (ideally) divides up. | ||
totalJobs := 20 | ||
|
||
for i := 0; i < 10; i++ { | ||
numWorkers := i + 1 | ||
elapsed := RunMultiPi(numWorkers, totalJobs) | ||
fmt.Printf("=====> %2d worker(s): %v\n\n", numWorkers, elapsed) | ||
|
||
// Give each trial a fresh start | ||
runtime.GC() | ||
} | ||
|
||
} | ||
|
||
var jobScript = ` | ||
pi = chud.pi_chudnovsky_bs(numDigits) | ||
last_5 = pi % 100000 | ||
print("%s: last 5 digits of %d is %d (job #%0d)" % (WORKER_ID, numDigits, last_5, jobID)) | ||
` | ||
|
||
var jobSrcTemplate = ` | ||
import pi_chudnovsky_bs as chud | ||
|
||
WORKER_ID = "{{WORKER_ID}}" | ||
|
||
print("%s ready!" % (WORKER_ID)) | ||
` | ||
|
||
type worker struct { | ||
name string | ||
ctx py.Context | ||
main *py.Module | ||
job *py.Code | ||
} | ||
|
||
func (w *worker) compileTemplate(pySrc string) { | ||
pySrc = strings.Replace(pySrc, "{{WORKER_ID}}", w.name, -1) | ||
|
||
mainImpl := py.ModuleImpl{ | ||
CodeSrc: pySrc, | ||
} | ||
|
||
var err error | ||
w.main, err = w.ctx.ModuleInit(&mainImpl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func RunMultiPi(numWorkers, numTimes int) time.Duration { | ||
var workersRunning sync.WaitGroup | ||
|
||
fmt.Printf("Starting %d worker(s) to calculate %d jobs...\n", numWorkers, numTimes) | ||
|
||
jobPipe := make(chan int) | ||
go func() { | ||
for i := 0; i < numTimes; i++ { | ||
jobPipe <- i + 1 | ||
} | ||
close(jobPipe) | ||
}() | ||
|
||
// Note that py.Code can be shared (accessed concurrently) since it is an inherently read-only object | ||
jobCode, err := py.Compile(jobScript, "<jobScript>", py.ExecMode, 0, true) | ||
if err != nil { | ||
log.Fatal("jobScript failed to comple") | ||
} | ||
|
||
workers := make([]worker, numWorkers) | ||
for i := 0; i < numWorkers; i++ { | ||
|
||
opts := py.DefaultContextOpts() | ||
|
||
// Make sure our import statement will find pi_chudnovsky_bs | ||
opts.SysPaths = append(opts.SysPaths, "..") | ||
|
||
workers[i] = worker{ | ||
name: fmt.Sprintf("Worker #%d", i+1), | ||
ctx: py.NewContext(opts), | ||
job: jobCode, | ||
} | ||
|
||
workersRunning.Add(1) | ||
} | ||
|
||
startTime := time.Now() | ||
|
||
for i := range workers { | ||
w := workers[i] | ||
go func() { | ||
|
||
// Compiling can be concurrent since there is no associated py.Context | ||
w.compileTemplate(jobSrcTemplate) | ||
|
||
for jobID := range jobPipe { | ||
numDigits := 100000 | ||
if jobID%2 == 0 { | ||
numDigits *= 10 | ||
} | ||
py.SetAttrString(w.main.Globals, "numDigits", py.Int(numDigits)) | ||
py.SetAttrString(w.main.Globals, "jobID", py.Int(jobID)) | ||
w.ctx.RunCode(jobCode, w.main.Globals, w.main.Globals, nil) | ||
} | ||
workersRunning.Done() | ||
}() | ||
} | ||
|
||
workersRunning.Wait() | ||
|
||
return time.Since(startTime) | ||
} |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.