module Thread::Object

Represents Python-style class interfacing thread.

Attributes

native_thread[RW]

Holds Ruby native thread instance. @return [Thread] Ruby native thread object

Public Instance Methods

alive?() click to toggle source

Indicates thread is alive. @param [Boolean] true if it is, false in otherwise

# File lib/thread/object.rb, line 77
def alive?
    @native_thread.alive?
end
log(message) click to toggle source

Logs an message. By overriding this, you can change the format.

@param [String] message message for writing out

# File lib/thread/object.rb, line 88
def log(message)
    STDERR.write "[" << Time.now.strftime("%Y-%m-%d %H:%M:%S") << "] " << self.class.name << ": " << message.to_s << "\n"
end
run() click to toggle source

Runs the thread code. @abstract

# File lib/thread/object.rb, line 31
def run
    raise Exception::new("Method #run must be overriden. It should contain body of the thread.")
end
shutdown!() click to toggle source

Shutdowns the thread.

# File lib/thread/object.rb, line 68
def shutdown!
    @native_thread.terminate()
end
start!(silent = nil) click to toggle source

Starts the thread by calling the #run method as in Python. It’s non-blocking, of sure.

Uncacthed rxceptions raised by thread are written out to STDERR by default. This feature can be turned of by the silent argument.

@param [nil, :silent] silent indicates, it shouln’t write

exceptions out

@return [Thread] native Ruby thread @see run

# File lib/thread/object.rb, line 49
def start!(silent = nil)
    @native_thread = Thread::new do
        begin
            self.run()
        rescue ::Exception => e
            if silent != :silent
                self.log "THREAD EXCEPTION! " << e.class.to_s << ": " << e.message
                e.backtrace.each { |i| self.log i }
            end
        end
    end
    
    return @native_thread
end