traject 0.11.0 → 0.12.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.
- data/lib/traject/thread_pool.rb +24 -7
- data/lib/traject/version.rb +1 -1
- metadata +1 -1
data/lib/traject/thread_pool.rb
CHANGED
@@ -74,30 +74,47 @@ module Traject
|
|
74
74
|
end
|
75
75
|
|
76
76
|
# Pass it a block, MAYBE gets executed in the bg in a thread pool. Maybe
|
77
|
-
# gets executed in the calling thread.
|
77
|
+
# gets executed in the calling thread.
|
78
78
|
#
|
79
79
|
# There are actually two 'maybes':
|
80
|
-
#
|
80
|
+
#
|
81
81
|
# * If Traject::ThreadPool was configured with null thread pool, then ALL
|
82
|
-
# work will be executed in calling thread.
|
82
|
+
# work will be executed in calling thread.
|
83
83
|
#
|
84
84
|
# * If there is a thread pool, but it's work queue is full, then a job
|
85
85
|
# will be executed in calling thread (because we configured our java
|
86
86
|
# thread pool with a limited sized queue, and CallerRunsPolicy rejection strategy)
|
87
|
-
|
87
|
+
#
|
88
|
+
# You can pass arbitrary arguments to the method, that will then be passed
|
89
|
+
# to your block -- similar to how ruby Thread.new works. This is convenient
|
90
|
+
# for creating variables unique to the block that won't be shared outside
|
91
|
+
# the thread:
|
92
|
+
#
|
93
|
+
# thread_pool.maybe_in_thread_pool(x, y) do |x1, y1|
|
94
|
+
# 100.times do
|
95
|
+
# something_with(x1)
|
96
|
+
# end
|
97
|
+
# end
|
98
|
+
# x = "someting else"
|
99
|
+
# # If we hadn't passed args with block, and had just
|
100
|
+
# # used x in the block, it'd be the SAME x as this one,
|
101
|
+
# # and would be pointing to a different string now!
|
102
|
+
#
|
103
|
+
# Note, that just makes block-local variables, it doesn't
|
104
|
+
# help you with whether a data structure itself is thread safe.
|
105
|
+
def maybe_in_thread_pool(*args)
|
88
106
|
start_t = Time.now
|
89
107
|
|
90
108
|
if @thread_pool
|
91
|
-
|
92
109
|
@thread_pool.execute do
|
93
110
|
begin
|
94
|
-
yield
|
111
|
+
yield(*args)
|
95
112
|
rescue Exception => e
|
96
113
|
collect_exception(e)
|
97
114
|
end
|
98
115
|
end
|
99
116
|
else
|
100
|
-
yield
|
117
|
+
yield(*args)
|
101
118
|
end
|
102
119
|
|
103
120
|
end
|
data/lib/traject/version.rb
CHANGED