Ruby - async_fu, ease of use of threads
I have been working with ruby not so long ago, but almost in the early days there was an urgent need to run long-playing functions that should not block the operation of the main program.
I did not find a ready-made and simple solution, therefore I began to reinvent the wheel.
At the moment, the library allows you to:
Usage example:
You can feel it here: GitHub
TODO
PS It is interesting to listen to the guru :)
UPD
The above examples are not a way to use, this is a thread check, a piece of the old test.
In fact, you can do this:
I did not find a ready-made and simple solution, therefore I began to reinvent the wheel.
At the moment, the library allows you to:
- organize asynchronous method calls of your class
- guarantees the execution of all threads before exiting the program
Usage example:
Copy Source | Copy HTML- class YourClass1
- def hello
- p 'start'
- p 'list ' + Thread.list.join( ' ')
- p 'main ' + Thread.main.to_s
- p 'this ' + Thread.current.to_s
- p 'end'
- end
- end
-
- af = AsyncFu.new(YourClass1.new)
- af.hello
Copy Source | Copy HTML- class YourClass2 < AsyncFu
- def hello
- p 'start'
- p 'list ' + Thread.list.join( ' ')
- p 'main ' + Thread.main.to_s
- p 'this ' + Thread.current.to_s
- p 'end'
- end
- end
-
- ai = YourClass2.new
- ai.hello
You can feel it here: GitHub
TODO
- Make sane callback so that you can track the status.
- Make exception handling and status returns.
- Make it possible to use mixin style.
PS It is interesting to listen to the guru :)
UPD
The above examples are not a way to use, this is a thread check, a piece of the old test.
In fact, you can do this:
Copy Source | Copy HTML- require 'rubygems'
- require 'async_fu'
-
- class Some
- def grep(query, path)
- list = `grep -rne '#{query}' #{path}`
- File.new( '/tmp/grep.log', 'w' ).write list
- end
- def tick
- loop{
- sleep 1
- p 'tick'
- }
- end
- def tack
- loop{
- sleep 2
- p 'tack'
- }
- end
- end
-
- test = AsyncFu.new(Some.new)
- test.tick
- test.grep( 'thread.rb', '/usr/local/lib' )
- test.tack