简体   繁体   English

我可以在Rails应用程序中编写JRuby脚本,然后仅将JRuby用于该脚本/类吗?

[英]Is it possible for me to write a JRuby script within my Rails app and use JRuby just for that script/class?

I have a Rails application that is importing data from various third parties. 我有一个从各种第三方导入数据的Rails应用程序。 The jobs are taking a long time and I am looking into how I can use threads to speed this up. 这些工作需要很长时间,我正在研究如何使用线程来加快速度。 I know nothing about Java so apologies if this makes no sense. 我对Java一无所知,如果这没有道理,那么我深表歉意。

No, JRuby is an alternate Ruby interpreter, so you cannot "switch" to it in the middle of running MRI (the standard Ruby interpreter, written in C). 不,JRuby是备用的Ruby解释器,因此您不能在运行MRI(使用C语言编写的标准Ruby解释器)中“切换”到它。

You can create threads in MRI , but many people use a background job queue to handle this type of problem. 您可以在MRI中创建线程 ,但是许多人使用后台作业队列来处理此类问题。 If you really wanted to, you could also write a second application in JRuby that your first application made remote calls to. 如果确实愿意,您还可以在JRuby中编写第二个应用程序,第一个应用程序对其进行远程调用。

Yes, you can, as long as you intend to run it in a separate process like a rake task for example, 是的,您可以,只要您打算在单独的流程(例如rake任务)中运行它

in Gemfile you'll have to handle both options where gem sets are different, for example: Gemfile您必须同时处理宝石集不同的两个选项,例如:

platform :ruby do
  gem "pg"
  gem "ruby-oci8", '>= 2.1.4'
end

platform :jruby do
  gem "activerecord-jdbcpostgresql-adapter"
end

and you need to decide what to do with Gemfile.lock . 并且您需要决定如何处理Gemfile.lock One option is to keep 2 Gemfile.lock on git, one for JRuby gems and one for MRI Ruby gems - Gemfile.lock.mri and Gemfile.lock.jruby and then symlink accordingly when running bundle . 一种选择是在git上保留2个Gemfile.lock,一个用于JRuby gems,一个用于MRI Ruby gems- Gemfile.lock.mriGemfile.lock.jruby ,然后在运行bundle时相应地进行符号链接。

The other question is if you should , the above approach is a bit finicky and I'm generally using it for building gems or for playing with porting an app to JRuby. 另一个问题是,是否应该使用上述方法,所以上面的方法有点挑剔,我通常将其用于构建gem或将应用程序移植到JRuby。

Spawning multiple processes and background job queues is generally how things are done in MRI Ruby, it seems an overkill to introduce a whole new ruby for purpose of a single import task. 产生多个进程和后台作业队列通常是在MRI Ruby中完成工作的方式,为单个导入任务引入一个全新的红宝石似乎是过大的选择。

That said, I've had situations (on massive data imports done through ruby) where proper multithreading would have saved me a LOT of hassle, but in the end I always fell back to partitioning the problem and running it in separate processes (every odd id in one rake task, every even id in another, for example - bear in mind you might need to rebuild tables in database if you do this or possibly face very slippery regression issues, do a benchmark definitely). 就是说,我曾经遇到过这样的情况(通过ruby进行海量数据导入),适当的多线程本可以为我省去很多麻烦,但是最终我总是退一步将问题划分并在单独的进程中运行(每次都奇怪一个rake任务中的id,例如另一个id中的每个偶数id –请记住,如果您这样做可能会需要重建数据库中的表,否则可能会遇到非常滑倒的回归问题,因此一定要进行基准测试。

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

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