Ruby多线程性能问题 [英] Ruby multithreading Performance issues
问题描述
我正在构建Ruby应用程序。我有一套我想要灰度的图像。我的代码过去是这样的:
def Tools.grayscale_all_frames(frames_dir,output_dir)
number_of_frames = get_frames_count )
img_processor = ImageProcessor.new(frames_dir)
create_dir(output_dir)
for i in 1..number_of_frames
img_processor.load_image(frames_dir +/ frame_%04d .png%+ i)
img_processor.greyscale_image
img_processor.save_image_in_dir(output_dir,frame_%04d%+ i)
end
end
代码之后:
def Tools.greyscale_all_frames_threaded(frames_dir,output_dir)
number_of_frames = get_frames_count(frames_dir)
img_processor = ImageProcessor.new(frames_dir)
create_dir(output_dir)
greyscale_frames_threads = []
for frame_index in 1..3
greyscale_frames_threads<< Thread.new(frame_index){| frame_number |
puts载入图片#{frame_number}
img_processor.load_image(frames_dir +/ frame_%04d.png%+ frame_number)
img_processor.greyscale_image
img_processor.save_image_in_dir(output_dir ,frame_%04d%+ frame_number)
输入Greyscaled Image#{frame_number}
}
end
输入Starting Threads
greyscale_frames_threads.each {| thread | thread.join}
end
我想要的是一个线程被产生为每个图像。我有1000张图片。分辨率为1920 * 1080。所以我怎么看到的东西是这样的。我有一个线程数组,我称之为.join它。所以join会采取所有的线程,并开始他们,一个接一个?这是否意味着它将等待,直到线程1完成,然后开始线程2?
运行所有的线程在同时又不是一个接一个。所以在数学上,它会完成所有的1000帧,在同一时间它将需要完成1帧,对吗?
也可以有人解释我什么。
从我的理解.join将停止主线程,直到你的线程是或完成?
如果你不使用.join,那么线程将运行后台,主线程将继续。
那么使用.join的意义是什么呢?我想让我的主线程继续运行,并让其他线程在后台做东西?
感谢任何帮助/澄清!
因此
join
会占用所有线程,其他?
不,线程在调用 。
join
是的,它将暂停执行直到接收者(每个
那么使用
join
的意义是什么?
有时您希望并行启动一些任务,但必须等待每个任务完成才能继续。
我想我的主线程继续运行,并让后台的其他线程做东西
然后不要调用 join
。
毕竟它不是好主意并行启动1,000个线程。您的计算机只能在CPU可用的情况下并行运行多个任务。因此,不是开始1000个线程,而是将您的作业/任务放入队列/池中,并使用一些工作线程(CPU数=工人数)处理它们。
I am building Ruby application. I have a set of images that I want to greyscale. My code used to be like this:
def Tools.grayscale_all_frames(frames_dir,output_dir)
number_of_frames = get_frames_count(frames_dir)
img_processor = ImageProcessor.new(frames_dir)
create_dir(output_dir)
for i in 1..number_of_frames
img_processor.load_image(frames_dir+"/frame_%04d.png"%+i)
img_processor.greyscale_image
img_processor.save_image_in_dir(output_dir,"frame_%04d"%+i)
end
end
after threading the code:
def Tools.greyscale_all_frames_threaded(frames_dir,output_dir)
number_of_frames = get_frames_count(frames_dir)
img_processor = ImageProcessor.new(frames_dir)
create_dir(output_dir)
greyscale_frames_threads = []
for frame_index in 1..3
greyscale_frames_threads << Thread.new(frame_index) { |frame_number|
puts "Loading Image #{frame_number}"
img_processor.load_image(frames_dir+"/frame_%04d.png"%+frame_number)
img_processor.greyscale_image
img_processor.save_image_in_dir(output_dir,"frame_%04d"%+frame_number)
puts "Greyscaled Image #{frame_number}"
}
end
puts "Starting Threads"
greyscale_frames_threads.each { |thread| thread.join }
end
What I expected is a thread being spawned for each image. I have 1000 images. The resolution is 1920*1080. So how I see things is like this. I have an array of threads that I call .join on it. So join will take all the threads and start them, one after the other? Does that mean that it will wait until thread 1 is done and then start thread 2? What is the point of multithreading then?
What I want is this:
Run all the threads at the same time and not one after the other. So mathematically, it will finish all the 1000 frames in the same time it will take to finish 1 frame, right?
Also can somebody explain me what .join does? From my understanding .join will stop the main thread until your thread(s) is or are done? If you don't use .join, then the thread will run the background and the main thread will just continue.
So what is the point of using .join? I want my main thread to continue running and have the other threads in the background doing stuff?
Thanks for any help/clarification!!
So
join
will take all the threads and start them, one after the other?
No, the threads are started when invoking Thread#new
. It creates a new thread and executed the given block within that thread.
join
will stop the main thread until your thread(s) is or are done?
Yes, it will suspend execution until the receiver (each of your threads) exists.
So what is the point of using
join
?
Sometimes you want to start some tasks in parallel but you have to wait for each task to finish before you can continue.
I want my main thread to continue running and have the other threads in the background doing stuff
Then don't call join
.
After all it's not a good idea to start 1,000 threads in parallel. Your machine is only capable of running as many tasks in parallel as CPUs are available. So instead of starting 1,000 threads, place your jobs / tasks in a queue / pool and process them using some worker threads (number of CPUs = number of workers).
这篇关于Ruby多线程性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!