Posted to tcl by bairui at Sun Feb 14 05:14:56 GMT 2016view raw

  1. #! /usr/bin/env tclsh
  2. # Barry Arthur, Feb 2016
  3. # butchered from http://wiki.tcl.tk/2770
  4.  
  5. # synopsis:
  6. # script path [path...]
  7. # Prints the system du information for each path given on the
  8. # command line.
  9.  
  10. # NOTE: This is only a toy for playing with threads
  11. # There MUST be some wrong approaches in this code,
  12. # and I'm looking for feedback & corrections.
  13.  
  14. package require Thread
  15. package require struct::list
  16.  
  17. set du_job_count 0
  18. set runner 0
  19. set runner_count 3
  20. foreach x [struct::list iota $runner_count] {
  21. set runners($x) [thread::create {
  22. proc run {caller_id command} {
  23. try {
  24. set result [{*}$command]
  25. } on error {result opts} {
  26. # spurious, fragile and specific to du command on linux
  27. set result [concat {*}[lrange [split $result "\n"] end-1 end-1]]
  28. }
  29. thread::send -async $caller_id [list ::print_result $result]
  30. }
  31. thread::wait
  32. }]
  33. }
  34.  
  35. proc print_result result {
  36. puts $result
  37. incr ::du_job_count -1
  38. if {$::du_job_count == 0} {
  39. set ::done 1
  40. }
  41. }
  42.  
  43. proc du {path} {
  44. global runners runner_count runner du_job_count
  45. incr du_job_count
  46. set runner [expr [incr runner] % $runner_count]
  47. set command_string "exec -ignorestderr du -sk"
  48. lappend command_string $path
  49. lappend command_string "2>/dev/null"
  50. thread::send -async $runners($runner) [list run [thread::id] $command_string]
  51. }
  52.  
  53. lmap path $argv {du $path}
  54. vwait done
  55.  

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