# Rubyinline and rails - do they play nice

**URL:** <https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853>\
**Category:** Ruby\
**Created:** [September 15, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853 "2006-09-15T21:19:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 15, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/1 "2006-09-15T21:19:15Z")

</div>

I am not sure, if this deserves a response, but i was experimenting with  
rubyinline and was trying to write some of the controller code in C.

class FameController \< ApplicationController  
inline do |builder|  
builder.c "  
long factorial(int max) {  
int i=max, result=1;  
while (i \>= 2) { result \*= i–; }  
return result;  
}"  
end

def my\_factorial\_fame  
my\_fame = factorial(1)  
end

end

Now good thing about the above controller code is that, it works. And  
the  
bad news is, it works only once…upon the next refresh it says:

require on /home/gnufied/.ruby\_inline/Inline\_FoobarController\_cb89.so  
failed

I am not sure, how it translates into English. but SO file is there in  
the location mentioned. I guess, its completely crazy to attempt this.  
But i need such a thing.  
I have 3000 lines of ruby code that does some very complicated  
mathematical calculations and its slow. worst it generates  
segmentation faults(Probably writing it in C will  
generate some more of seg faults and no it was not written by me. I  
would have used GSL in places at least.)

Now…what are my options? this 3000 line code is a rails controller  
code.

Another question is, can i use rubyinline in background worker threads  
created by backgroundrb plugin?

PS: Sorry for a bag of mixed questions.

## –

The Road goes ever on and on  
Down from the door where it began.  
Now far ahead the Road has gone,  
And I must follow, if I can,  
Pursuing it with eager feet,  
Until it joins some larger way  
Where many paths and errands meet.  
And whither then? I cannot say.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 15, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/2 "2006-09-15T21:31:16Z")

</div>

Hi !

2006/9/15, Hemant . [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Now good thing about the above controller code is that, it works. And the  
> bad news is, it works only once…upon the next refresh it says:
> 
> require on /home/gnufied/.ruby\_inline/Inline\_FoobarController\_cb89.so failed

You have to remember that Rails reloads it’s controllers on every  
request in development mode. That’s probably your problem.

Couple of options I see:

1. Move the code into a file that won’t be reloaded on every request  
(lib/ for example ?)
2. Use BackgrounDRb as you suggest
3. Web Services or something

Hope that helps !

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 15, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/3 "2006-09-15T22:01:18Z")

</div>

On 9/16/06, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > You have to remember that Rails reloads it’s controllers on every  
> > request in development mode. That’s probably your problem.
> > 
> > Couple of options I see:
> > 
> > 1. Move the code into a file that won’t be reloaded on every request  
> > (lib/ for example ?)
> > 2. Use BackgrounDRb as you suggest
> > 3. Web Services or something

I am already using Backgroundrb for several other stuff. But i am  
curious, that how it will work with rubyinline? Ruby threads, should  
handle it alright…theoretically(since backgroundrb uses ruby threads)

But, i would just ask opinions of Ezra on this. Because i feel, stuff  
at my end needs some real optimization. These worker threads are damn  
slow and i need to make them fast.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 18, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/4 "2006-09-18T21:14:51Z")

</div>

On Sep 15, 2006, at 1:00 PM, hemant wrote:

> On 9/16/06, hemant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > 1. Use BackgrounDRb as you suggest
> 
> I am already using Backgroundrb for several other stuff. But i am  
> curious, that how it will work with rubyinline? Ruby threads, should  
> handle it alright…theoretically(since backgroundrb uses ruby threads)
> 
> But, i would just ask opinions of Ezra on this. Because i feel, stuff  
> at my end needs some real optimization. These worker threads are damn  
> slow and i need to make them fast.

Note that C code typically won’t allow a thread switch, so the ruby  
process will be blocked until the C method returns. Exceptions to  
this include socket operations performed via the Ruby/C API and  
manual calls of CHECK\_INTS.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 19, 2006, 11:26am UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/5 "2006-09-19T11:26:29Z")

</div>

On 9/19/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Note that C code typically won’t allow a thread switch, so the ruby  
> process will be blocked until the C method returns. Exceptions to  
> this include socket operations performed via the Ruby/C API and  
> manual calls of CHECK\_INTS.

oh…so until if i call some method defined inside a rubyinline  
function from a Ruby thread, the thread will block…till that method  
finished execution?

I will keep in mind.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 15, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/6 "2006-09-15T21:51:17Z")

</div>

> You have to remember that Rails reloads it’s controllers on every  
> request in development mode. That’s probably your problem.
> 
> Couple of options I see:
> 
> 1. Move the code into a file that won’t be reloaded on every request  
> (lib/ for example ?)
> 2. Use BackgrounDRb as you suggest
> 3. Web Services or something

Oh…yes, i forgot that. After running the stuff in production mode,  
it worked nicely. so i guess it would be ok to use rubyinline in  
rails.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 19, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/rubyinline-and-rails-do-they-play-nice/68853/7 "2006-09-19T17:56:24Z")

</div>

On Sep 19, 2006, at 2:26 AM, hemant wrote:

> oh…so until if i call some method defined inside a rubyinline  
> function from a Ruby thread, the thread will block…till that method  
> finished execution?
> 
> I will keep in mind.

So long as you stay inside the C function, yes. If you call back out  
to ruby or use socket operations threads can be switched.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
