Search code examples
rubymultithreadingconcurrencyrubygemssinatra

Ruby running two scripts with mulithreading


So I'm trying to have my ruby (no rails) application be run with a single call from the terminal i.e. 'ruby run.rb'. However I have two scripts that need to be run, app.rb and app2.rb, the issue is, both these scripts don't finish - they keep on being run so as to keep the system running, this means that one of the scripts never gets run - it calls the first script (app.rb) and never the second (app2.rb) these scripts need to be run concurrently!

It does work when I open another command line and just run one script in each however.

I have tried:

def runApp
    system("ruby app.rb")
end
def runApp2
    system("ruby app2.rb")
end
t1 = Thread.new{runApp()}
t2 = Thread.new{runApp2()}
t1.join
t2.join

However this will only run the first thread (the one running app.rb) because this is being constantly run. Any ideas how it can also run the second thread concurrently?

:EDIT: One of the scripts is using the Sinatra gem, the other also calls one of its functions every ten seconds.


Solution

  • So one possible solution I've found is

    system("ruby app.rb & ruby app2.rb")

    This only works however if running from linux I think however so I would still appreciate any further solutions.