1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sync.pool: simplify usages of pool.work_on_items

This commit is contained in:
Alexander Medvednikov 2020-03-05 18:37:57 +01:00 committed by GitHub
parent 9c1e50b1aa
commit a8e45251c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 9 deletions

View File

@ -45,5 +45,5 @@ fn main() {
// cases is what you want anyway... You can override the automatic choice // cases is what you want anyway... You can override the automatic choice
// by setting the VJOBS environment variable too. // by setting the VJOBS environment variable too.
// fetcher_pool.set_max_jobs( 4 ) // fetcher_pool.set_max_jobs( 4 )
fetcher_pool.work_on_items<int>(ids) fetcher_pool.work_on_items(ids)
} }

View File

@ -49,7 +49,7 @@ fn test_all_v_repl_files() {
// See: https://docs.microsoft.com/en-us/cpp/build/reference/fs-force-synchronous-pdb-writes?view=vs-2019 // See: https://docs.microsoft.com/en-us/cpp/build/reference/fs-force-synchronous-pdb-writes?view=vs-2019
pool_repl.set_max_jobs(1) pool_repl.set_max_jobs(1)
} }
pool_repl.work_on_items<string>(session.options.files) pool_repl.work_on_items(session.options.files)
session.bmark.stop() session.bmark.stop()
println(session.bmark.total_message('total time spent running REPL files')) println(session.bmark.total_message('total time spent running REPL files'))
} }

View File

@ -3,12 +3,17 @@ module sync
// * of items in parallel, without worrying about waitgroups, mutexes and so on. // * of items in parallel, without worrying about waitgroups, mutexes and so on.
// * // *
// * Usage example: // * Usage example:
// * pool := sync.new_pool_processor({ callback: worker_cb }) // * struct SResult{ s string }
// * //pool.work_on_items<string>(['a','b','c']) // TODO: vfmt and generics // * fn sprocess(p &sync.PoolProcessor, idx, wid int) voidptr {
// * pool.work_on_pointers(['a','b','c'].pointers()) // * item := p.get_item<string>(idx)
// * println('idx: $idx, wid: $wid, item: ' + item)
// * return &SResult{ item.reverse() }
// * }
// * pool := sync.new_pool_processor({ callback: sprocess })
// * pool.work_on_items(['a','b','c','d','e','f','g'])
// * // optionally, you can iterate over the results too: // * // optionally, you can iterate over the results too:
// * for x in pool.get_results<IResult>() { // * for x in pool.get_results<SResult>() {
// * // do stuff with x // * println('result: $x.s')
// * } // * }
// * // *
// * See https://github.com/vlang/v/blob/master/vlib/sync/pool_test.v for a // * See https://github.com/vlang/v/blob/master/vlib/sync/pool_test.v for a

View File

@ -35,7 +35,7 @@ fn test_work_on_strings() {
callback: worker_s callback: worker_s
maxjobs: 8 maxjobs: 8
}) })
pool_s.work_on_items<string>(['a','b','c','d','e','f','g','h','i','j']) pool_s.work_on_items(['a','b','c','d','e','f','g','h','i','j'])
for x in pool_s.get_results<SResult>() { for x in pool_s.get_results<SResult>() {
println( x.s ) println( x.s )
assert x.s.len > 1 assert x.s.len > 1
@ -50,7 +50,7 @@ fn test_work_on_ints() {
mut pool_i := sync.new_pool_processor({ mut pool_i := sync.new_pool_processor({
callback: worker_i callback: worker_i
}) })
pool_i.work_on_items<int>([1,2,3,4,5,6,7,8]) pool_i.work_on_items([1,2,3,4,5,6,7,8])
for x in pool_i.get_results<IResult>() { for x in pool_i.get_results<IResult>() {
println( x.i ) println( x.i )
assert x.i > 100 assert x.i > 100