本文簡要介紹ruby語言中 Ractor::ClosedError類
的用法。
當嘗試將消息發送到關閉的端口或從關閉的空端口檢索消息時引發。端口可以使用 Ractor#close_outgoing
/close_incoming 顯式關閉,並在 Ractor
終止時隱式關閉。
r = Ractor.new { sleep(500) }
r.close_outgoing
r.take # Ractor::ClosedError
ClosedError
是 StopIteration
的後代,因此關閉 ractor 將打破循環而不傳播錯誤:
r = Ractor.new do
loop do
msg = receive # raises ClosedError and loop traps it
puts "Received: #{msg}"
end
puts "loop exited"
end
3.times{|i| r << i}
r.close_incoming
r.take
puts "Continue successfully"
這將打印:
Received: 0 Received: 1 Received: 2 loop exited Continue successfully
相關用法
- Ruby Closure類用法及代碼示例
- Ruby Class類用法及代碼示例
- Ruby Class.superclass用法及代碼示例
- Ruby Class.inherited用法及代碼示例
- Ruby Class.new用法及代碼示例
- Ruby Class.allocate用法及代碼示例
- Ruby Class.file用法及代碼示例
- Ruby Class.subclasses用法及代碼示例
- Ruby CStructEntity.[]=用法及代碼示例
- Ruby Context.save_history=用法及代碼示例
- Ruby CSV.header_convert用法及代碼示例
- Ruby Constants模塊用法及代碼示例
- Ruby CMath tanh()用法及代碼示例
- Ruby CSV.skip_lines用法及代碼示例
- Ruby CGI.new用法及代碼示例
- Ruby Comparable.between?用法及代碼示例
- Ruby CGI.print用法及代碼示例
- Ruby CGI.http_header用法及代碼示例
- Ruby CMath cos()用法及代碼示例
- Ruby Complex.arg用法及代碼示例
- Ruby CSV.table用法及代碼示例
- Ruby CSV.force_quotes?用法及代碼示例
- Ruby CParser模塊用法及代碼示例
- Ruby CSV.unconverted_fields?用法及代碼示例
- Ruby ComposedSet類用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 ClosedError類。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。