Posted to tcl by apn at Fri Jan 08 05:55:34 GMT 2016view raw
- Example session of checking urls in parallel (*async, non-blocking*) using promises:
- % set urls [list http://www.google.com http://127.0.0.1:1234 http://www.yahoo.com]
- http://www.google.com http://127.0.0.1:1234 http://www.yahoo.com
- % set gate [checkurls $urls]
- ::oo::Obj65
- % $gate done puts
- {http://www.google.com/ ok} {http://127.0.0.1:1234/ error} {http://www.yahoo.com/ ok}
- % $gate destroy
- And the actual promise-based code:
- package require http
- package require lambda
- proc checkurl {url} {
- set prom [promise::Promise new]
- http::geturl $url -method HEAD -command [lambda {prom tok} {
- upvar #0 $tok url_state
- $prom fulfill [list $url_state(url) $url_state(status)]
- ::http::cleanup $tok
- } $prom]
- return $prom
- }
- proc checkurls {urls} {
- return [promise::all [lmap url $urls {checkurl $url}]]
- }