Ruby Parallel Every Cycle

I have the following code:

FTP ... do |ftp| files.each do |file| ... ftp.put(file) sleep 1 end end 

I would like to run each file in a separate thread or parallel path. What is the right way to do this? Is it correct?

Here is my parallel pearl attempt

 FTP ... do |ftp| Parallel.map(files) do |file| ... ftp.put(file) sleep 1 end end 

A parallel transmission / output problem can occur simultaneously:

 as = [1,2,3,4,5,6,7,8] results = Parallel.map(as) do |a| puts a end 

How can I make puts appear as usual, dividing the line.

+6
source share
2 answers

The entire parallelization point must be performed simultaneously. But if some part of the process in which you want to run some code, you can use mutex like:

 semaphore = Mutex.new as = [1,2,3,4,5,6,7,8] results = Parallel.map(as, in_threads: 3) do |a| # Parallel stuff sleep rand semaphore.synchronize { # Sequential stuff puts a } # Parallel stuff sleep rand end 

You will see that it prints the material correctly, but not necessarily in the same order. I used in_threads instead of in_processes (by default) because mutex does not work with processes. See below for an alternative if you need processes.

Literature:

+1
source

In the interest of simplicity, here's what I would do with the built-in Thread :

 results = files.map do |file| result = Thread.new do ftp.put(file) end end 

Please note that this code assumes ftp.put(file) safely returned. If this is not guaranteed, you will have to do it yourself by wrapping the calls in the timeout block and each thread will return an exception if it is thrown, and then at the very end of the loop there is a lock to see that the results do not contain exceptions.

0
source

All Articles