Posted to tcl by pooryorick at Fri Dec 14 18:09:22 GMT 2012view raw

  1. #! /bin/env tclsh
  2.  
  3. proc morecowgets {count url} {
  4. variable wgets
  5. while {[incr count -1] > -1} {
  6. set chan [open "|wget $url"]
  7. chan configure $chan -blocking false
  8. chan event $chan readable [list reading $chan]
  9. dict set wgets $chan alive
  10. }
  11. }
  12.  
  13. proc reading {chan} {
  14. variable wgets
  15. if {[eof $chan]} {
  16. close $chan
  17. puts "unsetting $chan"
  18. dict unset wgets $chan
  19. } else {
  20. read $chan
  21. }
  22. }
  23.  
  24. proc wgets args {
  25. puts "howdy"
  26. variable total
  27. variable wgets
  28. set count 20
  29. if {![dict size $wgets]} {
  30. puts "new batch!"
  31. if {[incr total $count] > 100} {
  32. variable end true
  33. }
  34. morecowgets $count http://www.google.com
  35. }
  36. }
  37.  
  38. variable total 0
  39. trace add variable [namespace current]::wgets write wgets
  40. #this triggers the trace
  41. variable wgets [dict create]
  42. vwait [namespace current]::end
  43.