Posted to tcl by bairui at Sun Feb 14 05:14:56 GMT 2016view pretty
#! /usr/bin/env tclsh # Barry Arthur, Feb 2016 # butchered from http://wiki.tcl.tk/2770 # synopsis: # script path [path...] # Prints the system du information for each path given on the # command line. # NOTE: This is only a toy for playing with threads # There MUST be some wrong approaches in this code, # and I'm looking for feedback & corrections. package require Thread package require struct::list set du_job_count 0 set runner 0 set runner_count 3 foreach x [struct::list iota $runner_count] { set runners($x) [thread::create { proc run {caller_id command} { try { set result [{*}$command] } on error {result opts} { # spurious, fragile and specific to du command on linux set result [concat {*}[lrange [split $result "\n"] end-1 end-1]] } thread::send -async $caller_id [list ::print_result $result] } thread::wait }] } proc print_result result { puts $result incr ::du_job_count -1 if {$::du_job_count == 0} { set ::done 1 } } proc du {path} { global runners runner_count runner du_job_count incr du_job_count set runner [expr [incr runner] % $runner_count] set command_string "exec -ignorestderr du -sk" lappend command_string $path lappend command_string "2>/dev/null" thread::send -async $runners($runner) [list run [thread::id] $command_string] } lmap path $argv {du $path} vwait done
Comments
Posted by apn at Sun Feb 14 05:59:57 GMT 2016 [text] [code]
set promises [lmap path $paths { promise::pexec du -sk $path }] set all_done [promise::all $promises] $all_done then [promise::lambda {outputs} { foreach output $outputs {puts $output} }]
Posted by apn at Sun Feb 14 06:03:44 GMT 2016 [text] [code]
See http://www.magicsplat.com/blog/promises-by-example/ for more info