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

looking for more high level documentation #342

Closed
rful011 opened this issue Mar 24, 2024 · 1 comment
Closed

looking for more high level documentation #342

rful011 opened this issue Mar 24, 2024 · 1 comment

Comments

@rful011
Copy link

rful011 commented Mar 24, 2024

I have just spent 2 days hard slog (reading source, trial and lots of error ; ) to figuring out how to get Parallel and Listen to play nicely together to implement a program that picks up files from a spool directory and processes them, in parallel if needed.

I note that there is no mention of using a Proc as a source in the readme???

I have been doing this in a single thread for years but realised that the spool files were building up at busy times of the day.

To save anyone else who wants to have the ability to use a proc source I suggest adding this to the readme:

source may be a Proc that returns the next job. This example gets jobs from the Listen gem:

\`\`\`Ruby
job_queue = []
queue_access = Proc.new { job_queue.pop }

listener = Listen.to '.' do |modified, added, removed|
  if added
    added.each do |fn|
      job_queue << fn
    end
  end
end
listener.start # not blocking                                                                                                                                                                           
Parallel.map( queue_access, in_processes: 3 ) { |fn| fn ? process_file(iblogs, fn, dispose, extra) : sleep( interval )}
 \`\`\`
@grosser
Copy link
Owner

grosser commented Mar 24, 2024

with threads it would be easy since you could use in_threads(3) and just pop from the queue

added an example here #344

@grosser grosser closed this as completed Mar 24, 2024
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

No branches or pull requests

2 participants