简体   繁体   English

为什么我需要在 Ruby on Rails 应用程序中包装线程?

[英]Why do I need to wrap threads in Ruby on rails application?

In my RoR app, I'm writing an API in which I need to call multiple upstream APIs, so I'm planning to call them in parallel to save time.在我的 RoR 应用程序中,我正在编写一个 API,其中我需要调用多个上游 API,因此我计划并行调用它们以节省时间。 I want to follow best practices when implementing multi-threaded logic in ruby-on-rails applications.在 ruby-on-rails 应用程序中实现多线程逻辑时,我想遵循最佳实践。

The RoR guide states clearly that we need to wrap our code but it didn't explain why it is important. RoR 指南清楚地指出我们需要包装我们的代码,但它没有解释为什么它很重要。

From ruby-on-rails guidelines :来自ruby-on-rails 指南

Each thread should be wrapped before it runs application code, so if your application manually delegates work to other threads, such as via Thread.new or Concurrent Ruby features that use thread pools, you should immediately wrap the block每个线程在运行应用程序代码之前都应该被包装,因此如果您的应用程序手动将工作委派给其他线程,例如通过使用线程池的 Thread.new 或 Concurrent Ruby 功能,您应该立即包装该块

  • My App runs Rails version 4.我的应用程序运行 Rails 版本 4。
  • Number of upstream API calls in a single request ranges from 3 to 30单次请求上游API调用次数3-30
  • I checked out this similar SO post , but it doesn't mention anything about wrapping threaded code.我查看了这篇类似的 SO 帖子,但它没有提及任何关于wrapping线程代码的内容。

Wrapping the thread on the executor makes sure that you won't have any problem with unloaded constants... You won't see errors like将线程包装在执行程序上可确保您不会遇到卸载常量的任何问题......您不会看到类似这样的错误

Unable to autoload constant User, expected ../user.rb to define it (LoadError).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM