You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Method `<<` is used in method `handle_servers`: `pool << client` in Puma::Server#run.
2120
2123
# `pool << client` means add a request to the thread pool,
2121
-
# and then the thread waked up will process the request.
2124
+
# and then the waked up thread will process the request.
2122
2125
not_empty.wait mutex
2123
2126
2124
2127
@waiting-=1
@@ -2136,7 +2139,7 @@ module Puma
2136
2139
2137
2140
begin
2138
2141
# `block.call` will switch program to the block definition part.
2139
-
# The Block definition part is in `Puma::Server#run`:
2142
+
# The block definition part is in `Puma::Server#run`:
2140
2143
# @thread_pool = ThreadPool.new(@min_threads,
2141
2144
# @max_threads,
2142
2145
# IOBuffer) do |client, buffer| #...; end
@@ -2191,7 +2194,7 @@ module Puma
2191
2194
2192
2195
# Wake up the waiting thread to process the request.
2193
2196
# The waiting thread is defined in the same file: Puma::ThreadPool#spawn_thread.
2194
-
#There are these code in `spawn_thread`:
2197
+
#This code is in `spawn_thread`:
2195
2198
# while true
2196
2199
# # ...
2197
2200
# not_empty.wait mutex
@@ -2200,7 +2203,7 @@ module Puma
2200
2203
# end
2201
2204
@not_empty.signal
2202
2205
end
2203
-
end
2206
+
end
2204
2207
end
2205
2208
end
2206
2209
```
@@ -2212,7 +2215,7 @@ In `#perform`, call `Rails::Server#start`. Then call `Rack::Server#start`.
2212
2215
2213
2216
Then call `Rack::Handler::Puma.run(YourProject::Application.new)`.
2214
2217
2215
-
In `.run`, Puma will new a always running Thread to`ios = IO.select(sockets)`.
2218
+
In `.run`, Puma will new a always running Thread for`ios = IO.selectsockets`.
2216
2219
2217
2220
Request is created from `ios` object.
2218
2221
@@ -2222,14 +2225,15 @@ The thread will invoke rack apps' `call` to get the response for the request.
2222
2225
2223
2226
### Exiting Puma
2224
2227
#### Process and Thread
2225
-
For Puma is multiple threads, we need to have some basic concepts about Process and Thread.
2228
+
Because Puma is using multiple threads, we need to have some basic concepts about Process and Thread.
2226
2229
2227
-
This link's good for you to obtain the concepts: [Process and Thread](https://stackoverflow.com/questions/4894609/will-a-cpu-process-have-at-least-one-thread)
2230
+
This link is good for you to obtain the concepts: [Process and Thread](https://stackoverflow.com/questions/4894609/will-a-cpu-process-have-at-least-one-thread)
2228
2231
2229
2232
In the next part, you will often see `thread.join`.
2230
2233
2231
-
I will use a simple example to tell what does `thread.join` do.
2234
+
I will use two simple example to tell what does `thread.join` do.
2232
2235
2236
+
##### Example one
2233
2237
Try to run `test_thread_join.rb`.
2234
2238
2235
2239
```ruby
@@ -2256,6 +2260,32 @@ After you added `thread.join`, you can see:
2256
2260
````
2257
2261
in console.
2258
2262
2263
+
##### Example two
2264
+
```ruby
2265
+
arr = [
2266
+
Thread.new { sleep 1 },
2267
+
Thread.new do
2268
+
sleep 5
2269
+
puts 'I am arry[1]'
2270
+
end,
2271
+
Thread.new { sleep 8}
2272
+
]
2273
+
2274
+
puts Thread.list.size # returns 4 (including the main thread)
2275
+
2276
+
sleep 2
2277
+
2278
+
arr.each { |thread| puts "~~~~~ #{thread}" }
2279
+
2280
+
puts Thread.list.size # returns 3 (because arr[0] is dead)
2281
+
2282
+
# arr[1].join # comment off to see differences
2283
+
2284
+
arr.each { |thread| puts "~~~~~ #{thread}" }
2285
+
2286
+
puts "Exit main thread"
2287
+
```
2288
+
2259
2289
#### Send `SIGTERM` to Puma
2260
2290
When you stop puma by running `$ kill -s SIGTERM puma_process_id`, you will enter `setup_signals`in`Puma::Launcher#run`.
2261
2291
```ruby
@@ -2267,7 +2297,7 @@ module Puma
2267
2297
def run
2268
2298
#...
2269
2299
2270
-
# Set the behavior for signals like `$ kill -s SIGTERM process_id`.
2300
+
# Set the behaviors for signals like `$ kill -s SIGTERM process_id`.
2271
2301
setup_signals # Let's step into this line.
2272
2302
2273
2303
set_process_title
@@ -2277,7 +2307,7 @@ module Puma
2277
2307
# ...
2278
2308
end
2279
2309
2280
-
# Set the behavior for signals like `$ kill -s SIGTERM process_id`.
2310
+
# Set the behaviors for signals like `$ kill -s SIGTERM process_id`.
0 commit comments