Question

I am using Rails3 with Puma on JRuby 1.7 with threaded mode ( config.threadsafe!) enabled.

Now as the theory goes: For multiple client requests which come in, a new thread will be used to serve the user rather than a new rails process being started every time.

If I want to find the id of this new request thread, how can I do so?

Was it helpful?

Solution 2

for this purpose, you will find following methods useful:

Thread.current to gets the of thread which is currently being executed

#<Thread:0x8301ef4 run>

Thread.list to get an array of Thread objects for all threads that are either runnable or stopped.

[
    [0] #<Thread:0x8301ef4 run>
]

Hope it helps

OTHER TIPS

... finding a "unique" thread id of a currently executing Thread the ruby-way :

Thread.current.object_id

this will work in all rubies however it might not be really "unique" ... see some servers (and puma might do that as well) might re-use "native" threads (e.g. using a fixed thread pool) thus the id might actually be the same for 2 requests executing one after the other.

depending on the implementation details it might be a different ruby Thread (although it's unlikely) object but under JRuby will be the same Java Thread ... you can get the "native" java.lang.Thread id using :

JRuby.reference(Thread.current).native_thread.id

in general if you need something truly unique per request basis you should base it on e.g. the (Rack/Rails) Request object itself which is not reused (recycled) by the application/server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top