Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save spytheman/eef2dbfa519acaf023d3eb68f4935960 to your computer and use it in GitHub Desktop.

Select an option

Save spytheman/eef2dbfa519acaf023d3eb68f4935960 to your computer and use it in GitHub Desktop.
import time
import runtime
// these are here just as a harness to test the threads/channel setup below:
struct Dataset {
data string
}
struct Options {
x int
}
struct VerifyResult {
mut:
worker_id int
y int
}
fn cross_validate(ds &Dataset, opts Options) VerifyResult {
return VerifyResult{
y: ds.data.len + opts.x * 10
}
}
// setup for the go routines/processing threads/channels
type ChanneledOptions = Options | bool
fn option_worker(worker_id int, ds &Dataset, work_channel chan ChanneledOptions, result_channel chan VerifyResult) {
mut processed := 0
for {
x := <-work_channel
match x {
Options {
time.sleep(100 * time.millisecond)
mut result := cross_validate(ds, x)
result.worker_id = worker_id
result_channel <- result
processed++
}
bool {
eprint('>>>>>>>>>> worker_id: $worker_id exiting, processed: $processed\n')
return
}
}
}
}
fn main() {
ds := Dataset{'some data'}
///
mut all_options := []ChanneledOptions{}
total_jobs := 20
for i in 0 .. total_jobs {
all_options << Options{i}
}
eprint('> total_jobs: $total_jobs\n')
max_parallel_jobs := runtime.nr_jobs()
eprint('> max_parallel_jobs: $max_parallel_jobs\n')
// adding sentinel values, so the option_worker threads can exit without further coordination
for i in 0 .. max_parallel_jobs {
all_options << false
eprint('> adding sentinel $i\n')
}
mut work_channel := chan ChanneledOptions{cap: all_options.len + max_parallel_jobs}
mut result_channel := chan VerifyResult{}
dump(work_channel)
for o in all_options {
work_channel <- o
}
eprint('> added all $all_options.len\n')
for i in 0 .. max_parallel_jobs {
go option_worker(i, &ds, work_channel, result_channel)
}
for i := 0; i < total_jobs; i++ {
result := <-result_channel
eprint('>>>>> result $i: $result\n')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment