我正在尝试在 EventMachine 中集成一些阻塞库/操作,并且我考虑将此类代码封装在包含 EM::Deferrable 的类中。在可延迟对象中包含这样的代码是否有意义:
class Whatever
include EM::Deferrable
def some_operation
result = some_blocking_operations
if result.considered_success?
succeed(result)
else
fail(result)
end
end
end
还是我应该坚持:
op = lambda do
result = some_blocking_operations
end
cb = lambda do |res|
# do some kind of if here to check if it's success or failure
end
EM.defer(op,cb)
就个人而言,我更喜欢第一个,因为对我来说它读起来更好一些。在这种情况下实施 deferable 是否有意义?