一次处理 n 个项目(使用线程)

发布于 2024-08-09 22:59:24 字数 657 浏览 4 评论 0原文

我正在做很多人可能需要做的事情,处理执行时间可变的任务。我有以下概念验证代码:

threads = []

(1...10000).each do |n|
  threads << Thread.new do
    run_for = rand(10)
    puts "Starting thread #{n}(#{run_for})"
    time=Time.new
    while 1 do
      if Time.new - time >= run_for then
          break
      else
          sleep 1
      end
    end
    puts "Ending thread #{n}(#{run_for})"
  end
  finished_threads = []
  while threads.size >= 10 do
    threads.each do |t|
      finished_threads << t unless t.alive?
    end
    finished_threads.each do |t|
      threads.delete(t)
    end
  end
end

它不会启动新线程,直到先前的线程之一消失。有谁知道更好、更优雅的方法吗?

I'm doing what a lot of people probably need to do, processing tasks that have a variable execution time. I have the following proof of concept code:

threads = []

(1...10000).each do |n|
  threads << Thread.new do
    run_for = rand(10)
    puts "Starting thread #{n}(#{run_for})"
    time=Time.new
    while 1 do
      if Time.new - time >= run_for then
          break
      else
          sleep 1
      end
    end
    puts "Ending thread #{n}(#{run_for})"
  end
  finished_threads = []
  while threads.size >= 10 do
    threads.each do |t|
      finished_threads << t unless t.alive?
    end
    finished_threads.each do |t|
      threads.delete(t)
    end
  end
end

It doesn't start a new thread until one of the previous threads has dropped off. Does anyone know a better, more elegant way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-08-16 22:59:24

我建议创建一个工作池。请参阅http://snippets.dzone.com/posts/show/3276。然后将所有可变长度工作提交到池中,并调用 join 等待所有线程完成。

I'd suggest creating a work pool. See http://snippets.dzone.com/posts/show/3276. Then submit all of your variable length work to the pool, and call join to wait for all the threads to complete.

放我走吧 2024-08-16 22:59:24

work_queue gem 是在应用程序中异步并发执行任务的最简单方法。

wq = WorkQueue.new 2 # Limit the maximum number of simultaneous worker threads

(1..10_000).each do
  wq.enqueue_b do
    # Task
  end
end

wq.join # All tasks are complete after this

The work_queue gem is the easiest way to perform tasks asynchronously and concurrently in your application.

wq = WorkQueue.new 2 # Limit the maximum number of simultaneous worker threads

(1..10_000).each do
  wq.enqueue_b do
    # Task
  end
end

wq.join # All tasks are complete after this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文