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

#! /bin/env tclsh

proc morecowgets {count url} {
        variable wgets
        while {[incr count -1] > -1} {
                set chan [open "|wget $url"]
                chan configure $chan -blocking false
                chan event $chan readable [list reading $chan]
                dict set wgets $chan alive
        }
}

proc reading {chan} {
        variable wgets
        if {[eof $chan]} {
                close $chan
                puts "unsetting $chan"
                dict unset wgets $chan
        } else {
                read $chan
        }
}

proc wgets args {
        puts "howdy"
        variable total
        variable wgets
        set count 20
        if {![dict size $wgets]} {
                puts "new batch!"
                if {[incr total $count] > 100} {
                        variable end true
                }
                morecowgets $count http://www.google.com
        }
}

variable total 0
trace add variable [namespace current]::wgets write wgets
#this triggers the trace
variable wgets [dict create]
vwait [namespace current]::end