我正在使用台风,我想提出一个单一的要求,而不妨碍回应。稍后,我可能会检查响应,也可能不会。关键是我不希望代码执行等待响应。
有没有办法把这个内置到台风中?
否则我想我得用线程自己去做了?
发布于 2015-11-02 00:39:09
您可以尝试使用线程
response = nil
request_thread = Thread.new {
# Set up the request object here
response = request.response
}在那里,您可以检查response == nil,查看请求是否已经发出,并且可以调用request_thread.join来阻塞,直到线程完成执行为止。
发布于 2017-04-03 14:23:58
我建议查看Ruby的“唯一”宝石。
据我所知,台风阻塞了“hydra.run”号呼叫
使用Unirest,它不会阻塞get / post / put / etc调用,而是继续运行。如果需要,可以将“对象”存储在哈希或数组中,其中包含一个标识符,以便稍后检索,如下所示:
identifier_requests['id'] = Unirest.post(url,headers: headers, parameters: param, auth: auth)然后,若要阻止或检索响应,请使用对响应对象的一个调用:
response_code = (identifier_requests['id']).code
response.bodyhttp://unirest.io/ruby.html
发布于 2020-05-06 16:11:45
台风有内置的非阻塞呼叫。从他们的医生那里:
request = Typhoeus::Request.new("www.example.com", followlocation: true)
request.on_complete do |response|
if response.success?
# hell yeah
elsif response.timed_out?
# aw hell no
log("got a time out")
elsif response.code == 0
# Could not get an http response, something's wrong.
log(response.return_message)
else
# Received a non-successful http response.
log("HTTP request failed: " + response.code.to_s)
end
end
request.runhttps://stackoverflow.com/questions/33467946
复制相似问题