I'm considering moving over to Torquebox, but I need to set the nice level of one of the external daemons i want to change to a Service to 19. Is there any equivalent way of doing this in Torquebox, so that a service has lower run time priority than anything else, especially the 'web' part of Torquebox?

有帮助吗?

解决方案

Services are just POROs conforming to API TorqueBox can work with. TorqueBox expects you to yield his thread back as soon as possible after calling Service#start, which is why services are customarily run within separate worker threads and you can set priority of the worker thread using Thread#priority (see https://github.com/jruby/jruby/wiki/DifferencesBetweenMriAndJruby#thread-priority) - which is the Thread equivalent of nice

Should be as simple as:

class Service
  def start
    @worker = Thread.new do
      # do servicy stuff
    end
    @worker.priority = ...
  end

  def stop
    @worker.stop
  end

end

However, Java seems to suffer some quirks on this that are OS dependant (see Setting priority to Java's threads and Throttling CPU from within Java) and JRuby inherits those quirks.

Having those quirks in mind, if all else fails, you can always do sleep 0.01 every now and then to stagger execution a bit and yield CPU time to other jobs, you can do this deterministically (sleep 0.01 if count%100==0) or you can do this non-deterministically for pretty much same effect and simpler code (sleep 0.01 if random(100)==0) - and play with the exact number (1, 10, 100, 1000, ...),

as far as I can tell EventMachine uses sleep 0.01 in the reactor thread to prevent pegging the CPU, this approach is not as outlandish as it sounds at first.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top