我如何管理 ruby 线程以便它们完成所有工作?
我有一个可以分为独立单元的计算,我现在处理它的方式是创建固定数量的线程,然后移交每个线程中要完成的工作块。因此,在伪代码中,它看起来像这样:
# main thread
work_units.take(10).each {|work_unit| spawn_thread_for work_unit}
def spawn_thread_for(work)
Thread.new do
do_some work
more_work = work_units.pop
spawn_thread_for more_work unless more_work.nil?
end
end
基本上,一旦创建了初始数量的线程,每个线程都会执行一些工作,然后继续从工作堆栈中获取要完成的工作,直到没有剩余。当我在 irb 中运行时一切正常,但是当我使用解释器执行脚本时,一切都运行得不太好。我不知道如何让主线程等待所有工作完成。有没有一种好的方法可以做到这一点,或者我是否坚持在主线程中执行 sleep 10 Until work_units.empty?
I have a computation that can be divided into independent units and the way I'm dealing with it now is by creating a fixed number of threads and then handing off chunks of work to be done in each thread. So in pseudo code here's what it looks like
# main thread
work_units.take(10).each {|work_unit| spawn_thread_for work_unit}
def spawn_thread_for(work)
Thread.new do
do_some work
more_work = work_units.pop
spawn_thread_for more_work unless more_work.nil?
end
end
Basically once the initial number of threads is created each one does some work and then keeps taking stuff to be done from the work stack until nothing is left. Everything works fine when I run things in irb but when I execute the script using the interpreter things don't work out so well. I'm not sure how to make the main thread wait until all the work is finished. Is there a nice way of doing this or am I stuck with executing sleep 10 until work_units.empty?
in the main thread
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 ruby 1.9(和 2.0)中,您可以使用 stdlib 中的 ThreadsWait 来实现此目的:
In ruby 1.9 (and 2.0), you can use
ThreadsWait
from the stdlib for this purpose:如果修改
spawn_thread_for
以保存对创建的Thread
的引用,则可以在线程上调用Thread#join
来等待完成: :(
从
ri Thread.new
文档中窃取。有关更多详细信息,请参阅ri Thread.join
文档。)因此,如果您修改
spawn_thread_for
保存线程引用,你可以加入它们:(未经测试,但应该有味道)
If you modify
spawn_thread_for
to save a reference to your createdThread
, then you can callThread#join
on the thread to wait for completion:produces:
(Stolen from the
ri Thread.new
documentation. See theri Thread.join
documentation for some more details.)So, if you amend
spawn_thread_for
to save the Thread references, you can join on them all:(Untested, but ought to give the flavor)
看起来您正在复制 Parallel Each (Peach) 库提供的内容。
It seems like you are replicating what the Parallel Each (Peach) library provides.
您可以使用 Thread#join
join(p1 = v1) public
您还可以使用 Enumerable#each_slice 批量迭代工作单元
You can use Thread#join
join(p1 = v1) public
Also you can use Enumerable#each_slice to iterate over the work units in batches