Call a method in a thread

I guess it’s possible to run an entire JRuby program in a thread (let’s
call it thread B) if the thread “requires” a .rb file that contains the
program with its classes and methods…

Is it possible from outside the thread (i.e from thread A) to call one
of the methods in thread B? And if so, how?

…R

Robin -

Basically, any code can be run in any thread (though the wisdom of doing
so might be questionable). There’s no need for a separate file. In
case it’s helpful, slide #28 of my JRuby slideshow at
https://speakerdeck.com/keithrbennett/jruby-the-synergy-of-ruby-and-the-jvm
shows an example of threading that has a block specified inline in the
call to Thread.new.

One way of having a different thread execute a function is to create a
queue from which the worker thread receives “job assignments”. The
dispatching thread posts a message to that queue that communicates what
it is that needs to be done, and any data it may need. When the worker
thread gets around to reading that message (there may be other messages
posted before it), it executes the requested job.

If this doesn’t help, can you provide a specific example of when you’d
want to do this?

  • Keith

Keith R. Bennett
http://about.me/keithrbennett
Work Status: Available for Consulting, Direct Employment

Thanks Keith,
Very interesting presentation which I’ve bookmarked. May I make a few
comments in another Topic?

===anyway, back to this Topic=======

I guess I wasn’t very clear in my opening post.

Suppose I have a file called prog1.rb containing (inter alia)


def showStuff(stuff)
puts stuff
end

I would like to be able to run

tt = Thread.new do
require ‘prog1.rb’
end

And then in another thread I would like to be able to do something which
is the same as showStuff(“This is a test”).

I am thinking of a concept a bit like the way ScriptingContainer can
call methods in a script - but all entirely in JRuby.

…R

Keith B. wrote in post #1093771:
Robin -

Basically, any code can be run in any thread (though the wisdom of doing
so might be questionable). There’s no need for a separate file.

  • Keith

On Friday, January 25, 2013 5:29:07 AM UTC-7, Robin McKay wrote:

I guess it’s possible to run an entire JRuby program in a thread (let’s
call it thread B) if the thread “requires” a .rb file that contains the
program with its classes and methods…

Is it possible from outside the thread (i.e from thread A) to call one
of the methods in thread B? And if so, how?

Have you looked at this thread library?
https://github.com/celluloid/celluloid/wiki/Basic-usage

Thanks Keith,
It sounds like what I thought of doing is not practical or more trouble
than it’s worth.

…R

Keith B. wrote in post #1093911:
Robin -

as would be running each in a ScriptingContainer. Any classes you load
(or, even worse, modify) would be shared by all threads. And there
could be variables that would be shared as well (class variables, static
variables, etc.).

Robin -


Keith R. Bennett
http://about.me/keithrbennett

On Jan 26, 2013, at 4:07 AM, Robin McKay [email protected] wrote:

Thanks Keith,
Very interesting presentation which I’ve bookmarked.

Thanks. If you’d like the audio of the presentation, the audio and
slideshow are both at
http://www.bbs-software.com/blog/2012/09/04/jruby-presentation-northern-virginia-ruby-user-group/.
Nothing special, but there might be some useful pieces of information.
More below…

def showStuff(stuff)
And then in another thread I would like to be able to do something which
is the same as showStuff(“This is a test”).

I am thinking of a concept a bit like the way ScriptingContainer can
call methods in a script - but all entirely in JRuby.

It sounds like what you’re really looking to do is eval or load, not
require, right? Require will load the file once per Ruby VM lifetime, so
subsequent uses would have no effect.

As I understand it, you want to evaluate an arbitrary chunk of code, and
do it in its own thread. You could do that, but doing that in threads as
you describe is not as effective in isolating the jobs from each other
as would be running each in a ScriptingContainer. Any classes you load
(or, even worse, modify) would be shared by all threads. And there
could be variables that would be shared as well (class variables, static
variables, etc.).

For more protection, you’d want each job to have its own Ruby virtual
machine, not just its own thread. If, on the other hand, you had control
over the types of jobs that were requested, sharing the Ruby VM might
not be a bad thing.

  • Keith

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs