📜 ⬆️ ⬇️

Ruby - async_fu, easy to use threads

I’ve been working with ruby ​​for a while but almost in the first 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:


Example of use:
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
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  1. class YourClass2 <AsyncFu
  2. def hello
  3. p 'start'
  4. p 'list' + Thread .list.join ( '' )
  5. p 'main' + Thread .main.to_s
  6. p 'this' + Thread .current.to_s
  7. p 'end'
  8. end
  9. end
  10. ai = YourClass2 .new
  11. ai. hello

Feel free here: GitHub
')
Todo


PS It is interesting to listen to the guru :)

UPD
The above examples, there is no way to use it, this is a test of threads, a piece of old dough.
In fact, you can do this:

Copy Source | Copy HTML
  1. require 'rubygems'
  2. require 'async_fu'
  3. class some
  4. def grep (query, path)
  5. list = ` grep -rne '# {query}' # {path}`
  6. File . new ( '/tmp/grep.log' , 'w' ) .write list
  7. end
  8. def tick
  9. loop {
  10. sleep 1
  11. p 'tick'
  12. }
  13. end
  14. def tack
  15. loop {
  16. sleep 2
  17. p 'tack'
  18. }
  19. end
  20. end
  21. test = AsyncFu. new ( Some .new)
  22. test. tick
  23. test. grep ( 'thread.rb' , '/ usr / local / lib' )
  24. test. tack

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


All Articles