I am fairly new to ruby and also at writing multi-threaded apps, and I was
hoping someone with a better mastery of both might look at a problem I'm
having. :)
I am using the ruby module for fastcgi (available via the cvs repository
under lib/fcgi/) with threads and am having some strange results. This may
be because fcgi is not thread-safe (I notice that ENV and stdout are treated
as globals which wouldn't work in a threaded cgi setup), but the problems
appear to be unrelated to this.
Basically, the thread which services the browser connection sometimes stops
in the middle of the block of code and closes the connection to the browser.
The results are unpredictable: sometimes the thread finished successfully,
sometimes it doesn't.
I've included a copy of the cgi, but I think it might take someone with a
better understanding of threading issues to look at the fcgi module.
-----------
#!/usr/bin/env ruby
require "thread"
require "fcgi"
FCGI.each do |fcgi|
Thread.new(fcgi) { |f|
f.out.print "Content-type: text/html\r\n\r\n"
# print some arbitrary data so we can make sure the thread is finishing
f.out.print f.out.type, "<br>\n"
f.out.print f.out.id, "<br>\n"
f.out.methods.sort.each { |method| f.out.print "#{method}<br>\n" }
}
end
----------
To duplicate the problem, simply execute this through your browser and reload
the pages several times. The output should vary quite a bit from page to
page if you do it quickly.
Any suggestions would be appreciated.
Thanks,
-Brad
> FCGI.each do |fcgi|
> Thread.new(fcgi) { |f|
[snip]
> }
> end
You've created a thread, but you do not wait for it to finish what it
is doing. In general, the pattern for starting one or mroe threads
and allowing them to finish is
threads = []
while some_condition
threads << Thread.new {
}
end
threads.each { |t| t.join }
If you do not join your threads they will be terminated with extreme
prejudice when the main thread exits.
--
Jonathan Feinberg j...@pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf
In message "[ruby-talk:11918] problem with threads and fastcgi"
on 01/03/03, Brad Hilton <bhi...@vpop.net> writes:
|I am using the ruby module for fastcgi (available via the cvs repository
|under lib/fcgi/) with threads and am having some strange results. This may
|be because fcgi is not thread-safe (I notice that ENV and stdout are treated
|as globals which wouldn't work in a threaded cgi setup), but the problems
|appear to be unrelated to this.
fcgi is not thread-safe. Unlike other IO facilities, it does not try to
avoid IO blocking. I'll make fcgi thread-safe soon (or I hope someone
voluneer to update it).
matz.
> Brad Hilton <bhi...@vpop.net> writes:
> > FCGI.each do |fcgi|
> > Thread.new(fcgi) { |f|
> [snip]
> > }
> > end
> You've created a thread, but you do not wait for it to finish what it
> is doing. In general, the pattern for starting one or mroe threads
> and allowing them to finish is
> threads = []
> while some_condition
> threads << Thread.new {
> }
> end
> threads.each { |t| t.join }
> If you do not join your threads they will be terminated with extreme
> prejudice when the main thread exits.
FCGI.each is the equivalent to a never-ending while loop, so the main
thread never exits until the webserver reloads or stops. Thanks for the
followup, though.
-Brad