thread_runner 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ThreadRunner.rb +92 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41757ea5533df52d93a1a9cd65f41b17f80a1606cc804e187cc4999c27eff98a
4
+ data.tar.gz: d991ddcba94e3ba78300fbe71a137c28aebbe011d75ac89d77508b05956c2af3
5
+ SHA512:
6
+ metadata.gz: 1e10b8688d3dc903d14a7909de0afaf0b1d23b141eac96da8f7d65adbe442fa0f13b6a230f85ca903f53ce7cea78754a90888a22a66db21de83797acd9638fa8
7
+ data.tar.gz: 35d95f4237dfc2ef1755b03acf3b95dba763fe13c5ee7f58a04540410469b8473c0a87e8ace05444472a1cdfa3711e93a09aa3ca1d4cae936b95f9896bd20844
@@ -0,0 +1,92 @@
1
+ # basic thread library
2
+ class ThreadRunner
3
+ THREAD_COUNT = 1
4
+ MONITORING_TIME = 10 # as second
5
+
6
+ def initialize (
7
+ product_func: ,
8
+ thread_func: ,
9
+ thread_count: THREAD_COUNT,
10
+ monitoring_time: MONITORING_TIME,
11
+ init_func: -> {},
12
+ finish_func: -> {},
13
+ main_func: main_thread(),
14
+ monitor_func: monitor_thread(),
15
+ product_to_queue_func: products_to_queue()
16
+ )
17
+ @thread_func = thread_func
18
+ @product_func = product_func
19
+ @thread_count = thread_count
20
+ @monitoring_time = monitoring_time
21
+ @init_func = init_func
22
+ @finish_func = finish_func
23
+ @main_thread = main_func
24
+ @monitor_thread = monitor_func
25
+ @product_to_queue = product_to_queue_func
26
+ end
27
+
28
+ def run
29
+ @init_func.call
30
+
31
+ products = @product_func.call
32
+ queue = @product_to_queue.call(products)
33
+ @main_thread.call(queue)
34
+ @monitor_thread.call(queue)
35
+
36
+ @finish_func.call
37
+ end
38
+
39
+ private
40
+
41
+ # start threads for download operation
42
+ def main_thread
43
+ -> queue {
44
+ return false if @thread_count <= 0
45
+
46
+ threads = @thread_count.times.map { |tid| Thread.new { @thread_func.call(tid, queue) } }
47
+ threads.each(&:join)
48
+
49
+ # puts "No element left message!"
50
+ }
51
+ end
52
+
53
+ # turns lines into thread safe queue
54
+ def products_to_queue
55
+ -> products { products.inject(Queue.new) { |q, param| q<<param }}
56
+ end
57
+
58
+ # monitor
59
+ def monitor_thread
60
+ -> queue {
61
+ return false if @monitoring_time <= 0
62
+
63
+ while queue.size > 0
64
+ @monitoring_time.times {
65
+ if queue.size <= 0
66
+ break
67
+ end
68
+ sleep 1
69
+ }
70
+ # puts "#{queue.size} element left!" if queue.size > 0
71
+ end
72
+ }
73
+ end
74
+ end
75
+
76
+ runner = ThreadRunner.new(
77
+ product_func: -> {
78
+ [1,2,3,4,5,6,7,8,9]
79
+ },
80
+ thread_func: -> tid, queue {
81
+ until queue.size == 0
82
+ param = queue.pop
83
+ puts "thread: '#{tid}' processed param: '#{param}' !"
84
+ sleep 1
85
+ end
86
+ },
87
+ thread_count: 4
88
+ )
89
+
90
+
91
+
92
+ runner.run
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thread_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - tayak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Basic thread library with which you can manage initialize, main, monitor,
14
+ queue and finish functions.
15
+ email: yasir.kiroglu@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/ThreadRunner.rb
21
+ homepage: https://github.com/taiak/ThreadRunner
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.5.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Basic thread library with which you can manage initialize, main, monitor,
44
+ queue and finish functions.
45
+ test_files: []