Skip to content

Instantly share code, notes, and snippets.

@tarcieri
Created May 11, 2011 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarcieri/966031 to your computer and use it in GitHub Desktop.
Save tarcieri/966031 to your computer and use it in GitHub Desktop.
Fun with Net::HTTP and threads on Rubinius
rbx-head-hydra :001 > module Enumerable
rbx-head-hydra :002?> def pmap
rbx-head-hydra :003?> threads = map do |elem|
rbx-head-hydra :004 > Thread.new { yield elem }
rbx-head-hydra :005?> end
rbx-head-hydra :006?>
rbx-head-hydra :007 > threads.map { |thread| thread.join.value }
rbx-head-hydra :008?> end
rbx-head-hydra :009?> end
=> #<Rubinius::CompiledMethod pmap file=(irb)>
rbx-head-hydra :011 > term = 'dog'
rbx-head-hydra :013 > require 'open-uri'
=> true
rbx-head-hydra :014 > require 'cgi'
=> true
rbx-head-hydra :015 > %w(red yellow blue green).map { |color| page = open("http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}") { |sock| sock.read }; CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] }
=> ["http://www.southlakebreeders.com/images/clifford.png", "http://www.cartoonstock.com/lowres/wwe1084l.jpg", "http://chattahbox.com/images/2009/07/blue_dog.jpg", "http://iloapp.husngchallenge.com/blog/www?ShowFile&image=1298242578.jpg"]
rbx-head-hydra :016 > %w(red yellow blue green).pmap { |color| page = open("http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}") { |sock| sock.read }; CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] }
=> ["http://piedpatter.files.wordpress.com/2011/03/clifford-white-background.png", "http://www.cartoonstock.com/lowres/wwe1084l.jpg", "http://chattahbox.com/images/2009/07/blue_dog.jpg", "http://iloapp.husngchallenge.com/blog/www?ShowFile&image=1298242578.jpg"]
rbx-head-hydra :017 > %w(red yellow blue green).pmap { |color| page = open("http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}") { |sock| sock.read }; CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] }
ThreadError: timeout within critical session
from Timeout.timeout at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/timeout.rb:146
from Object(Net::BufferedIO)#timeout at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/timeout.rb:170
from Net::BufferedIO#rbuf_fill at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/protocol.rb:134
from Net::BufferedIO#read at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/protocol.rb:86
from Net::HTTPResponse(Net::HTTPOK)#read_chunked at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2232
from Net::HTTPResponse(Net::HTTPOK)#read_body_0 at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2207
from Net::HTTPResponse(Net::HTTPOK)#read_body at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2173
from { } in OpenURI.open_http at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:257
from { } in Net::HTTP#request at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:1053
from Net::HTTPResponse(Net::HTTPOK)#reading_body at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2136
from Net::HTTP#request at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:1052
from { } in OpenURI.open_http at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:248
from Net::HTTP#start at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:543
from OpenURI.open_http at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:242
from URI::HTTP#buffer_open at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:616
from { } in OpenURI.open_loop at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:164
from { } in Kernel(Module)#catch at kernel/common/throw_catch.rb:29
from Rubinius::ThrownValue.register at kernel/common/throw_catch.rb:8
from Kernel(Module)#catch at kernel/common/throw_catch.rb:28
from OpenURI.open_loop at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:162
from OpenURI.open_uri at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:132
from OpenURI::OpenRead(URI::HTTP)#open at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:518
from Kernel(Object)#open at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:30
from { } in Object#irb_binding at (irb):16
from { } in Enumerable(Array)#pmap at (irb):4
from Thread#__run__ at kernel/bootstrap/thread.rb:109
rbx-head-hydra :018 > %w(red yellow blue green).pmap { |color| page = open("http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}") { |sock| sock.read }; CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] }
=> ["http://www.southlakebreeders.com/images/clifford.png", "http://www.cartoonstock.com/lowres/wwe1084l.jpg", "http://chattahbox.com/images/2009/07/blue_dog.jpg", "http://iloapp.husngchallenge.com/blog/www?ShowFile&image=1298242578.jpg"]
rbx-head-hydra :019 > %w(red yellow blue green).pmap { |color| page = open("http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}") { |sock| sock.read }; CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] }
=> ["http://piedpatter.files.wordpress.com/2011/03/clifford-white-background.png", "http://www.cartoonstock.com/lowres/wwe1084l.jpg", "http://chattahbox.com/images/2009/07/blue_dog.jpg", "http://iloapp.husngchallenge.com/blog/www?ShowFile&image=1298242578.jpg"]
rbx-head-hydra :020 > %w(red yellow blue green).pmap { |color| page = open("http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}") { |sock| sock.read }; CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] }
ThreadError: timeout within critical session
from Timeout.timeout at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/timeout.rb:146
from Object(Net::BufferedIO)#timeout at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/timeout.rb:170
from Net::BufferedIO#rbuf_fill at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/protocol.rb:134
from Net::BufferedIO#read at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/protocol.rb:86
from Net::HTTPResponse(Net::HTTPOK)#read_chunked at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2232
from Net::HTTPResponse(Net::HTTPOK)#read_body_0 at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2207
from Net::HTTPResponse(Net::HTTPOK)#read_body at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2173
from { } in OpenURI.open_http at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:257
from { } in Net::HTTP#request at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:1053
from Net::HTTPResponse(Net::HTTPOK)#reading_body at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:2136
from Net::HTTP#request at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:1052
from { } in OpenURI.open_http at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:248
from Net::HTTP#start at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/net/http.rb:543
from OpenURI.open_http at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:242
from URI::HTTP#buffer_open at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:616
from { } in OpenURI.open_loop at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:164
from { } in Kernel(Module)#catch at kernel/common/throw_catch.rb:29
from Rubinius::ThrownValue.register at kernel/common/throw_catch.rb:8
from Kernel(Module)#catch at kernel/common/throw_catch.rb:28
from OpenURI.open_loop at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:162
from OpenURI.open_uri at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:132
from OpenURI::OpenRead(URI::HTTP)#open at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:518
from Kernel(Object)#open at /Users/tony/.rvm/rubies/rbx-head-hydra/lib/open-uri.rb:30
from { } in Object#irb_binding at (irb):16
from { } in Enumerable(Array)#pmap at (irb):4
from Thread#__run__ at kernel/bootstrap/thread.rb:109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment