Skip to content

Commit 6a0c11e

Browse files
author
Drew O'Meara
committed
license and formatting
1 parent 634823c commit 6a0c11e

3 files changed

Lines changed: 61 additions & 53 deletions

File tree

examples/multi-ctx/main.go

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
15
package main
26

37
import (
@@ -15,21 +19,20 @@ import (
1519
func main() {
1620

1721
// The total job count implies a fixed amount of work.
18-
// The number of workers is how many py.Ctx (in concurrent goroutines) to pull jobs off the queue.
19-
// One worker does all the work serially while N number of workers will (ideally) divide that up.
20-
totalJobs := 20
21-
22-
for i := 0; i < 10; i++ {
23-
numWorkers := i + 1
24-
elapsed := RunMultiPi(numWorkers, totalJobs)
25-
fmt.Printf("=====> %2d worker(s): %v\n\n", numWorkers, elapsed)
26-
27-
// Give each trial a fresh start
28-
runtime.GC()
29-
}
30-
31-
}
22+
// The number of workers is how many py.Ctx (in concurrent goroutines) to pull jobs off the queue.
23+
// One worker does all the work serially while N number of workers will (ideally) divide that up.
24+
totalJobs := 20
25+
26+
for i := 0; i < 10; i++ {
27+
numWorkers := i + 1
28+
elapsed := RunMultiPi(numWorkers, totalJobs)
29+
fmt.Printf("=====> %2d worker(s): %v\n\n", numWorkers, elapsed)
3230

31+
// Give each trial a fresh start
32+
runtime.GC()
33+
}
34+
35+
}
3336

3437
var jobScript = `
3538
pi = chud.pi_chudnovsky_bs(numDigits)
@@ -54,16 +57,16 @@ type worker struct {
5457

5558
func (w *worker) compileTemplate(pySrc string) {
5659
pySrc = strings.Replace(pySrc, "{{WORKER_ID}}", w.name, -1)
57-
58-
mainImpl := py.ModuleImpl{
59-
CodeSrc: pySrc,
60-
}
61-
62-
var err error
63-
w.main, err = w.ctx.ModuleInit(&mainImpl)
64-
if err != nil {
65-
log.Fatal(err)
66-
}
60+
61+
mainImpl := py.ModuleImpl{
62+
CodeSrc: pySrc,
63+
}
64+
65+
var err error
66+
w.main, err = w.ctx.ModuleInit(&mainImpl)
67+
if err != nil {
68+
log.Fatal(err)
69+
}
6770
}
6871

6972
func RunMultiPi(numWorkers, numTimes int) time.Duration {
@@ -74,60 +77,57 @@ func RunMultiPi(numWorkers, numTimes int) time.Duration {
7477
jobPipe := make(chan int)
7578
go func() {
7679
for i := 0; i < numTimes; i++ {
77-
jobPipe <- i+1
80+
jobPipe <- i + 1
7881
}
7982
close(jobPipe)
8083
}()
81-
84+
8285
// Note that py.Code can be shared (accessed concurrently) since it is an inherently read-only object
8386
jobCode, err := py.Compile(jobScript, "<jobScript>", py.ExecMode, 0, true)
8487
if err != nil {
85-
log.Fatal("jobScript failed to comple")
88+
log.Fatal("jobScript failed to comple")
8689
}
8790

8891
workers := make([]worker, numWorkers)
8992
for i := 0; i < numWorkers; i++ {
9093

91-
opts := py.DefaultCtxOpts()
92-
93-
// Make sure our import statement will find pi_chudnovsky_bs
94-
opts.SysPaths = append(opts.SysPaths, "..")
94+
opts := py.DefaultCtxOpts()
95+
96+
// Make sure our import statement will find pi_chudnovsky_bs
97+
opts.SysPaths = append(opts.SysPaths, "..")
9598

9699
workers[i] = worker{
97100
name: fmt.Sprintf("Worker #%d", i+1),
98101
ctx: py.NewCtx(opts),
99-
job: jobCode,
102+
job: jobCode,
100103
}
101-
104+
102105
workersRunning.Add(1)
103-
}
104-
105-
startTime := time.Now()
106-
106+
}
107+
108+
startTime := time.Now()
109+
107110
for i := range workers {
108-
w := workers[i]
111+
w := workers[i]
109112
go func() {
110-
111-
// Compiling can be concurrent since there is no associated py.Ctx
112-
w.compileTemplate(jobSrcTemplate)
113-
113+
114+
// Compiling can be concurrent since there is no associated py.Ctx
115+
w.compileTemplate(jobSrcTemplate)
116+
114117
for jobID := range jobPipe {
115-
numDigits := 100000
116-
if jobID % 2 == 0 {
117-
numDigits *= 10
118-
}
119-
py.SetAttrString(w.main.Globals, "numDigits", py.Int(numDigits))
120-
py.SetAttrString(w.main.Globals, "jobID", py.Int(jobID))
121-
w.ctx.RunCode(jobCode, w.main.Globals, w.main.Globals, nil)
118+
numDigits := 100000
119+
if jobID%2 == 0 {
120+
numDigits *= 10
121+
}
122+
py.SetAttrString(w.main.Globals, "numDigits", py.Int(numDigits))
123+
py.SetAttrString(w.main.Globals, "jobID", py.Int(jobID))
124+
w.ctx.RunCode(jobCode, w.main.Globals, w.main.Globals, nil)
122125
}
123126
workersRunning.Done()
124127
}()
125128
}
126129

127130
workersRunning.Wait()
128-
131+
129132
return time.Since(startTime)
130133
}
131-
132-
133-

py/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
15
package py
26

37
type CompileMode string

py/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
15
package py
26

37
import (

0 commit comments

Comments
 (0)