Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

ellcs
Copy link

@ellcs ellcs commented Oct 10, 2020

Implementation of ticket #273.

ellcs added 2 commits October 6, 2020 22:25
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)
Copy link
Owner

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
Copy link
Owner

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 ?

Suggested change
return exception if exception.class == Parallel::Return
return exception.result if exception.class == Parallel::Return

Comment on lines 306 to 316
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
Copy link
Owner

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 ?

Comment on lines +239 to +256
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

Copy link
Owner

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attr_reader :return_value
attr_reader :value

@grosser
Copy link
Owner

grosser commented Oct 12, 2020

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

@ellcs
Copy link
Author

ellcs commented Oct 12, 2020

Thanks for the review. I will take the time to apply some changes soon.

@grosser grosser mentioned this pull request Nov 7, 2020
@grosser grosser closed this Nov 7, 2020
@grosser
Copy link
Owner

grosser commented Nov 7, 2020

v1.20 has Parallel.map([1, 2, 3]) { |i| raise Parallel::Break, i if i == 2 } == 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants