Singleton initilized twice

M

maikonaraujo

HI
I have a little problem here, I am doing a webserver and I put some
calculations on the singleton's initialization and call the methods fo
this class to do some calculations for me (no active record, just
memory). The problem is the singleton is initilizing every method api
from webserver I call.
Can someone explain how can I solve this problem?

[]´s
 
R

Robert Klemme

I have a little problem here, I am doing a webserver and I put some
calculations on the singleton's initialization and call the methods fo
this class to do some calculations for me (no active record, just
memory). The problem is the singleton is initilizing every method api
from webserver I call.
Can someone explain how can I solve this problem?

Sounds like you are using CGI and thus your singleton is initialized
once for each CGI process. You need to find another mechanism to hold
your singleton - either you use FastCGI or mod-ruby or you create a
server process that is connected to via DRb from your CGI processes.

Kind regards

robert
 
M

maikonaraujo

Sounds like you are using CGI and thus your singleton is initialized
once for each CGI process. You need to find another mechanism to hold
your singleton - either you use FastCGI or mod-ruby or you create a
server process that is connected to via DRb from your CGI processes.

Kind regards

robert

Tks, I was doing a hello worl only ehehe, and runing "ruby script/
server" on the prompt.
 
D

Dan Teitsort

Tks, I was doing a hello worl only ehehe, and runing "ruby script/
server" on the prompt.

Then it sounds like you still have the problem. I've noticed the exact
same condition -- a singleton getting initialized twice -- and have
found a solution that works for me. (Sorry this is 10 months late, but
maybe it will help someone.)

I'm using 1.8.6-p36 on Windows.

In my case, the issue seemed related to the fact that 'require' behaves
badly if you happen to include the same file multiple times using
different path specifications (e.g., relative vs. absolute vs.
yet-another-relative). In those cases, the code in the file being
'required' gets executed each time a 'new' path formulation is used,
even though the same physical file is being referenced.

I addressed this by being careful to always File.expand_path(...) on the
path before doing a 'require', e.g.,

require File.expand_path('../../some_file_with_a_singleton_class.rb')

instead of

require '../../some_file_with_a_singleton_class.rb'

I should that, in my case, there was a _chance_ the multiple requires
were being done on different threads. But, offhand, I don't think they
were.


So, I guess the moral is: Singleton users, beware the require
'relative_path'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,275
Messages
2,571,382
Members
48,072
Latest member
FaustoBust

Latest Threads

Top