Launch applications with ruby ​​and multi-core support? (OSX)

I am looking for some guides / resources / tips that will show me how to run applications through ruby ​​script. I have several small tools that we use in our daily operations, which I want to solve with my tasks in one ruby ​​script.

I am basically trying to do the following:

  • run the application through ruby ​​script.
  • (waiting for the result) get the result code (success or error message)
  • if everything is ok, run the application in the next task.

Each of the tasks is also independent, so I would like to use 8 cores on my MacPro and run 8 instances at a time.

Any resources you could point me to would be greatly appreciated!

+4
source share
2 answers

First: use MacRuby. The latest MacRuby has built-in Grand Central built-in scheduling - that Spiffiness Snow Leopard automatically manages multicore streams, etc. You may not need this, but if you know that it will only work on a Mac, why not?

Secondly: get the Pickaxe 1.9 publisher version (which programs Ruby 1.9) and read the entire chapter on threads, fibers, and processes.

Third: adapt what you learn to your needs, but I would probably approach this problem by unscrewing a new Thread for each task and then running a system or `backtick` call for each utility in its own thread. Ruby 1.9 has real threads (and MacRuby has the best ones), so they all work right away - and locking the interpreter doesn't matter at all if you mostly expect external processes.

(If you did not need to pay attention to the results of the calls and did not need to quote, the Pickaxe exec('foo') if fork.nil? would be even simpler. I have used it many times for things like support for creating multiple directories at once and etc.)

The Colin EventMachine proposal will also work as fast as you could if you implemented it correctly (and if your workflow made sense for events with asynchronous I / O), but would be much more complex and difficult. Since you are not trying to scale the entire universe, you just want to perform some local tasks, I think that threads will do their best and make for simpler, more readable and maintainable code.

+2
source

I just found this yesterday, it can help you - http://github.com/rightscale/right_popen , which depends on EventMachine: http://rubyeventmachine.com/ . Writing your script in the style of an event or "reactor" will help you get a little sick. Think about it by writing javascript with some callbacks.

Something like this (I am not familiar with EM, therefore pseudocode):

 do_something do |event| event.success{ next_script } event.failure{ report_failure } end 
+1
source

Source: https://habr.com/ru/post/1304627/


All Articles