-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Parallel::find and Parallel::find_result #283
Conversation
In addtion to Parallel::Kill and Parallel::Break, a new Exception class named Parallel::Return allows raising an Exception which wraps a result value. That wrapped result will be unwrapped. When a method expects a single result, the option `:return_one` must be provided, because a given Enumeration might not contain a matching element or result.
else | ||
work_in_processes(job_factory, options.merge(:count => size), &block) | ||
end | ||
work_direct(job_factory, options, &block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2-space plz
@@ -485,7 +524,8 @@ def process_incoming_jobs(read, write, job_factory, options, &block) | |||
end | |||
|
|||
def handle_exception(exception, results) | |||
return nil if [Parallel::Break, Parallel::Kill].include? exception.class | |||
return if [Parallel::Break, Parallel::Kill].include?(exception.class) | |||
return exception if exception.class == Parallel::Return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect this to return the value ?
return exception if exception.class == Parallel::Return | |
return exception.result if exception.class == Parallel::Return |
if results | ||
options[:return_results] ? results : source | ||
if options[:return_one] | ||
if results.is_a?(Return) | ||
results.return_value | ||
end | ||
elsif options[:return_results] | ||
results | ||
else | ||
source | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks like it's doing similar kind of logic to handle_exception
... can it be combined ?
def find_result(array, options = {}, &block) | ||
raise "You must provide a block when calling #first_result" if block.nil? | ||
map(array, options.merge({return_one: true})) do |*a| | ||
if result = block.call(*a) | ||
raise Parallel::Return.new(result) | ||
end | ||
end | ||
end | ||
|
||
def find(array, options = {}, &block) | ||
raise "You must provide a block when calling #find" if block.nil? | ||
map(array, options.merge({return_one: true})) do |*a| | ||
if block.call(*a) | ||
raise Parallel::Return.new(*a) | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe let's just do the raise Return
part first, so this is a smaller PR ?
@@ -14,6 +14,14 @@ class Break < StandardError | |||
class Kill < StandardError | |||
end | |||
|
|||
class Return < StandardError | |||
attr_reader :return_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attr_reader :return_value | |
attr_reader :value |
Logic looks right, but I think you stumbled into a problem area of the code there, I think we should do a cleanup first before adding a new feature, the handle_exception and results/source logic seems like they would go together |
Thanks for the review. I will take the time to apply some changes soon. |
v1.20 has |
Implementation of ticket #273.