Discussion:
[jruby-user] Eliminate Startup Delay ?
Robin McKay
2014-09-19 21:38:14 UTC
Permalink
I have been very tempted recently to give up JRuby in favour of Python.
Part of the reason is that I have been dabbling with Arduinos and the
Arduino community is more likely to be familiar with Python. And the
other part is the JRuby startup delay which is painfully obvious after
using Python for an hour.

Then it occurred to me that the startup delay could be avoided by
preventing JRuby from exiting when a program finished.

This short code is a first crude attempt to achieve that and it seems to
work. Indeed it runs the short test program so fast that I lose track of
whether I have pressed return. And it seems to catch load errors, syntax
errors, runtime errors and java errors (in a much larger program that I
tried).

I have little doubt that a more experienced JRuby programmer will see
scope for many improvements.

It would be nice if every possible exception could be trapped with a
single RESCUE clause - but I don't know how to do that.

==========

ScriptRunner.rb

[code]
def runScript
begin
# load is used because require would only load the script once
load $jscriptName
# it is assumed that the script contains a method called
scriptStart
scriptStart
rescue ScriptError # catches load and syntax errors
puts "SCRIPT ERROR A"
puts $!
rescue NativeException # catches Java errors
puts "SCRIPT ERROR B"
puts $!
rescue # catches runtime errors
puts "SCRIPT ERROR C"
puts $!
end
end

puts
puts "Starting ScriptRunner"

$jscriptName = "./test.rb"

while true
puts
puts "Press Return"
xxx = gets
runScript
puts "Done"
end
[/code]

=============

test.rb

[code]
def scriptStart
puts "Hey - here is testing"
xxx = 8 / 1
puts xxx
end
[/code]
--
Posted via http://www.ruby-forum.com/.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Chris Seaton
2014-09-20 01:13:31 UTC
Permalink
Hello Robin,

Your idea is in theory a good one, and it has been implemented in a couple of different ways as Nailgun and Drip https://github.com/jruby/jruby/wiki/Improving-startup-time. Your particular proposed solution, in Ruby, has additional drawbacks in that each script isn’t going to get a fresh interpreter state is it? Any modules, classes, global variables, threads, monkey patches etc created in one script will hang around for the next one.

Chris
Post by Robin McKay
I have been very tempted recently to give up JRuby in favour of Python.
Part of the reason is that I have been dabbling with Arduinos and the
Arduino community is more likely to be familiar with Python. And the
other part is the JRuby startup delay which is painfully obvious after
using Python for an hour.
Then it occurred to me that the startup delay could be avoided by
preventing JRuby from exiting when a program finished.
This short code is a first crude attempt to achieve that and it seems to
work. Indeed it runs the short test program so fast that I lose track of
whether I have pressed return. And it seems to catch load errors, syntax
errors, runtime errors and java errors (in a much larger program that I
tried).
I have little doubt that a more experienced JRuby programmer will see
scope for many improvements.
It would be nice if every possible exception could be trapped with a
single RESCUE clause - but I don't know how to do that.
==========
ScriptRunner.rb
[code]
def runScript
begin
# load is used because require would only load the script once
load $jscriptName
# it is assumed that the script contains a method called
scriptStart
scriptStart
rescue ScriptError # catches load and syntax errors
puts "SCRIPT ERROR A"
puts $!
rescue NativeException # catches Java errors
puts "SCRIPT ERROR B"
puts $!
rescue # catches runtime errors
puts "SCRIPT ERROR C"
puts $!
end
end
puts
puts "Starting ScriptRunner"
$jscriptName = "./test.rb"
while true
puts
puts "Press Return"
xxx = gets
runScript
puts "Done"
end
[/code]
=============
test.rb
[code]
def scriptStart
puts "Hey - here is testing"
xxx = 8 / 1
puts xxx
end
[/code]
--
Posted via http://www.ruby-forum.com/.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Robin McKay
2014-09-20 08:11:05 UTC
Permalink
Chris Seaton wrote in post #1158047:
Any
Post by Chris Seaton
modules, classes, global variables, threads, monkey patches etc created
in one script will hang around for the next one.
Agreed. I wasn't thinking of using this idea to run several different
programs but, rather, to be able to re-run the same program quickly
during development. For example to provide an immediate response to
stupid typos.

I haven't tried, but I guess you could start a different version of
ScriptRunner for each program you want to work with. That should keep
the variables etc separate.

The startup delay is much less relevant for a finished program that is
only started once per day, and my idea would have no role there.

I only came across Drip yesterday and it didn't seem to have any impact
on startup delay. I seem to remember trying Nailgun a long time ago and
it wouldn't work with whatever I was trying.

...R
--
Posted via http://www.ruby-forum.com/.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Loading...