Search code examples
rubytyphoeus

Can I make asynchronous requests with Ruby's Typhoeus?


I am using Typhoeus and would like to make a single request without blocking for the response. Later on, I might check the response, or I might not. The point is I don't want the code execution to wait for the response.

Is there a way to do this built-in to Typhoeus?

Otherwise I guess I have to use threads and do it myself?


Solution

  • You could try using a thread:

    response = nil
    
    request_thread = Thread.new {
      # Set up the request object here
      response = request.response
    }
    

    From there you can check response == nil to see if the request has been made yet, and you can call request_thread.join to block until the thread is done executing.