# Running multiple simultaneous interpreters/scripts

**URL:** <https://www.ruby-forum.com/t/running-multiple-simultaneous-interpreters-scripts/105731>\
**Category:** Ruby\
**Created:** [June 1, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/running-multiple-simultaneous-interpreters-scripts/105731 "2007-06-01T21:37:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [June 1, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/running-multiple-simultaneous-interpreters-scripts/105731/1 "2007-06-01T21:37:48Z")

</div>

I have a setup where cron is calling N instances of a particular Ruby  
script simultaneously. It seems that as N increases, the delay before  
each script executes its first line of code increases by a little less  
than N seconds. I’m not sure if this is an issue with cron or if it’s  
the cumulative startup time for N ruby interpreters. An ideas on how to  
speed this up to a near constant (~\<1sec) delay? Is there a way to run  
all N scripts simultaneously with a single interpreter process? Thanks.

I’m using 1.8.5 on fc6.

Earle

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [June 1, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/running-multiple-simultaneous-interpreters-scripts/105731/2 "2007-06-01T21:58:02Z")

</div>

Earle C. wrote:

> I have a setup where cron is calling N instances of a particular Ruby  
> script simultaneously. It seems that as N increases, the delay before  
> each script executes its first line of code increases by a little less  
> than N seconds. I’m not sure if this is an issue with cron or if it’s  
> the cumulative startup time for N ruby interpreters. An ideas on how to  
> speed this up to a near constant (~\<1sec) delay? Is there a way to run  
> all N scripts simultaneously with a single interpreter process? Thanks.
> 
> I’m using 1.8.5 on fc6.

Why not fork the N instances from one ruby script called by cron?

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [June 2, 2007, 11:03am UTC](https://www.ruby-forum.com/t/running-multiple-simultaneous-interpreters-scripts/105731/3 "2007-06-02T11:03:15Z")

</div>

On 01.06.2007 21:57, Joel VanderWerf wrote:

> Why not fork the N instances from one ruby script called by cron?

Or use threads to implement parallel execution. OP, what do your  
scripts do?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [June 2, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/running-multiple-simultaneous-interpreters-scripts/105731/4 "2007-06-02T15:01:42Z")

</div>

On Sat, Jun 02, 2007 at 06:00:43PM +0900, Robert K. wrote:

> > Earle C. wrote:
> > 
> > > I have a setup where cron is calling N instances of a particular Ruby  
> > > script simultaneously. It seems that as N increases, the delay before  
> > > each script executes its first line of code increases by a little less  
> > > than N seconds. I’m not sure if this is an issue with cron or if it’s  
> > > the cumulative startup time for N ruby interpreters. An ideas on how to  
> > > speed this up to a near constant (~\<1sec) delay? Is there a way to run  
> > > all N scripts simultaneously with a single interpreter process? Thanks.

Well, you can’t make a CPU run faster than itself. So let’s say for the  
sake  
of argument Ruby has a one-second startup time, and this is a CPU  
limitation. Then if you run five Ruby scripts in five separate  
interpreters,  
it’s going to take 5 CPU-seconds of CPU time to start them all. If they  
all  
share the CPU time equally, then it’ll be shared 5 ways, so it will take  
each of them 5 seconds of real time to execute their 1 CPU-second of  
work.

If the startup overhead is a problem to you, you could just try running  
all  
the scripts one after the other within the same Ruby interpreter:

#!/usr/bin/ruby

# allscripts.rb

require ‘script1.rb’  
require ‘script2.rb’  
require ‘script3.rb’  
require ‘script4.rb’  
require ‘script5.rb’

This may work as long as they are written in such a way that this  
doesn’t  
matter (e.g. they don’t pollute the interpreter so badly that the  
following  
one doesn’t run, or depend on atexit {} or the like to function)

Alternatively, you write a single ruby process which starts and then:  
(1) forks off a new child for each script, or  
(2) starts each script in a new thread.

Brian.
