我是新来的水晶,我试图产生纤维来检查状态是否完整。下面是一些代码:
def fiber_operations()
status = -1
spawn do
while status != 5
response = HTTP::Client.get "https://api.com/endpoint"
response_to_hash = JSON.parse response.body
status = response_to_hash["status"]
sleep 2.seconds
end
end
Fiber.yield
end
当我创建了一些这样的错误发生(在此之前似乎运行良好):
编辑:" spawn中未处理的异常:SSL_shutdown:操作正在进行中(Errno)"
添加了更多的信息,我认为一个玩具的例子可能是足够好的,但它可能与HTTP::客户端,所以我添加了它。我正在为一些api端点做GET,并以这种方式获得状态。也许如果一个GET正在进行中,另一个不能打开?如果有,如何做到这一点?
编辑2:您可能已经点击了crystal-lang/crystal#3168。
我写了这个程序来测试,它似乎可以工作:
require "http/client"
require "json"
status = -1
spawn do
while status != 5
puts "Pinging..."
response = HTTP::Client.get "https://randomapi.com/api/6de6abfedb24f889e0b5f675edc50deb?fmt=raw&sole"
puts "Pong"
response_to_hash = JSON.parse response.body
status = response_to_hash[0]["first"].to_s.size
pp status
end
puts "Finish!"
exit 0
end
Fiber.yield
puts "Post yield"
sleep 20000.seconds # so the main thread doesn't exit yet
我认为这与光纤没有多大关系,但与Crystal/OpenSSL的错误有关。也许您可以在spawn
块中rescue
异常,以查看实际的异常。
此"错误"与已完成操作的等待列表有关。应该通过https://github.com/crystal-lang/crystal/pull/4433
修复现在发布在Crystal 0.23.0