title: How do I properly use Threads to connect ping a url?
body: <p>I am trying to ping a large amount of urls and retrieve information regarding the certificate of the url. As I read in this thoughtbot article here <a href="https://thoughtbot.com/blog/untangling-ruby-threads" rel="nofollow noreferrer">Thoughtbot Threads</a> and others, I've read that the best way to do this is by using Threads. When I implement threads however, I keep running into Timeout errors and other problems for urls that I can retrieve successfully on their own. I've been told in another related question that I asked earlier that I should not use Timeout with Threads. However, the examples I see wrap API/NET::HTTP/TCPSocket calls in the Timeout block and based opn what I've read, that entire API/NET::HTTP/TCP Socket call will be nested within the Thread. Here is my code:</p>
  
  <pre><code>class SslClient
    attr_reader :url, :port, :timeout
  
    def initialize(url, port = '443', timeout = 30)
      @url = url
      @port = port
      @timeout = timeout
    end
  
    def ping_for_certificate_info
      context = OpenSSL::SSL::SSLContext.new
      certificates = nil
      verify_result = nil
      Timeout.timeout(timeout) do
        tcp_client = TCPSocket.new(url, port)
        ssl_client = OpenSSL::SSL::SSLSocket.new tcp_client, context
        ssl_client.hostname = url
        ssl_client.sync_close = true
        ssl_client.connect
        certificates = ssl_client.peer_cert_chain
        verify_result = ssl_client.verify_result
        tcp_client.close
      end
      {certificate: certificates.first, verify_result: verify_result }
    rescue =&gt; error
      puts url
      puts error.inspect
    end
  end
  
    [VERY LARGE LIST OF URLS].map do |url|
        Thread.new do
          ssl_client = SslClient.new(url)
          cert_info = ssl_client.ping_for_certificate_info
          puts cert_info
        end
      end.map(&amp;:value)
  </code></pre>
  
  <p>If you run this code in your terminal, you will see many Timeout errors and ERNNO:TIMEDOUT errors for sites like fandango.com, fandom.com, mcaffee.com, google.de etc that should return information. When I run these individually however I get the information I need. When I run them in the thread they tend to fail especially for domains that have a foreign domain name. What I'm asking is whether I am using Threads correctly. This snippet of code that I've pasted is part of a larger piece of code that interacts with ActiveRecord objects in rails depending on the results given. Am I using Timeout and Threads correctly? What do I need to do to make this work? Why would a ping work individually but not wrapped in a thread? Help would be greatly appreciated.</p>