这是这篇文章的最终尝试:
当我尝试在线程中使用rest-client进行呼叫时,它将失败并提前退出线程。当我在线程外运行它时,就可以了。
有什么想法吗?
require 'base64'
require 'json'
require 'rest-client'
require 'thwait'
test = Thread.new {
endpoint = "http://my/url"
headers = {'Authorization' => "Basic #{Base64.encode64("username:password")}", 'Accept' => 'Application/json', 'Content-Type' => 'Application/json'}
payload = JSON.parse("{\"documents\": { \"textField1\":\"asdf\" }}").to_json
# See the results of the above. Successful now (headers hash with expected encoded value, JSON string as expected)
puts headers
puts payload
# Expecting REST response on the console. Nothing appears on the console.
puts RestClient::Request.execute(:method => :post, :url => endpoint, :headers => headers, :payload => payload, :verify_ssl => false)
# Expecting simple text output to the console to see if execution reaches this point. Nothing appears on the console.
puts 'done'
}
# Where this is actually being used, ThreadsWait is used for thread management, so simulating that here
thwait = ThreadsWait.new(test)
thwait.next_wait
答案 0 :(得分:0)
您需要 Field f =Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
线程才能看到输出。如果您在主线程终止之前没有加入它,它将杀死它(see docs)。
join
答案 1 :(得分:0)
我在Ruby 2.5.7和rest-client版本2.0.2中遇到了这个问题。在升级到Rest-Client版本2.1.0之后,问题消失了。较旧版本的rest-client在Ruby 2.3.1上运行良好。