2021-02-11 11:55:23 +03:00
|
|
|
import time
|
|
|
|
import sync.pool
|
|
|
|
|
|
|
|
pub struct SResult {
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IResult {
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
2022-11-10 21:19:19 +03:00
|
|
|
fn worker_s(mut p pool.PoolProcessor, idx int, worker_id int) &SResult {
|
2021-02-11 11:55:23 +03:00
|
|
|
item := p.get_item<string>(idx)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('worker_s worker_id: ${worker_id} | idx: ${idx} | item: ${item}')
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(3 * time.millisecond)
|
2022-11-15 16:53:13 +03:00
|
|
|
return &SResult{'${item} ${item}'}
|
2021-02-11 11:55:23 +03:00
|
|
|
}
|
|
|
|
|
2022-11-10 21:19:19 +03:00
|
|
|
fn worker_i(mut p pool.PoolProcessor, idx int, worker_id int) &IResult {
|
2021-02-11 11:55:23 +03:00
|
|
|
item := p.get_item<int>(idx)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('worker_i worker_id: ${worker_id} | idx: ${idx} | item: ${item}')
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(5 * time.millisecond)
|
2021-02-11 11:55:23 +03:00
|
|
|
return &IResult{item * 1000}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_work_on_strings() {
|
|
|
|
mut pool_s := pool.new_pool_processor(
|
|
|
|
callback: worker_s
|
|
|
|
maxjobs: 8
|
|
|
|
)
|
|
|
|
|
|
|
|
pool_s.work_on_items(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
|
|
|
|
for x in pool_s.get_results<SResult>() {
|
|
|
|
println(x.s)
|
|
|
|
assert x.s.len > 1
|
|
|
|
}
|
2021-10-29 21:01:07 +03:00
|
|
|
println('---------- pool_s.get_results_ref: --------------')
|
|
|
|
for x in pool_s.get_results_ref<SResult>() {
|
|
|
|
println(x.s)
|
|
|
|
assert x.s.len > 1
|
|
|
|
}
|
2021-02-11 11:55:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_work_on_ints() {
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: since maxjobs is left empty here,
|
2021-02-11 11:55:23 +03:00
|
|
|
// the pool processor will use njobs = runtime.nr_jobs so that
|
|
|
|
// it will work optimally without overloading the system
|
|
|
|
mut pool_i := pool.new_pool_processor(
|
|
|
|
callback: worker_i
|
|
|
|
)
|
|
|
|
|
|
|
|
pool_i.work_on_items([1, 2, 3, 4, 5, 6, 7, 8])
|
|
|
|
for x in pool_i.get_results<IResult>() {
|
|
|
|
println(x.i)
|
|
|
|
assert x.i > 100
|
|
|
|
}
|
2021-10-29 21:01:07 +03:00
|
|
|
println('---------- pool_i.get_results_ref: --------------')
|
|
|
|
for x in pool_i.get_results_ref<IResult>() {
|
|
|
|
println(x.i)
|
|
|
|
assert x.i > 100
|
|
|
|
}
|
2021-02-11 11:55:23 +03:00
|
|
|
}
|