[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Singleton initilized twice

maikonaraujo@gmail.com

6/3/2007 3:57:00 PM

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

3 Answers

Robert Klemme

6/3/2007 5:00:00 PM

0

On 03.06.2007 17:57, maikonaraujo@gmail.com wrote:
> 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

maikonaraujo@gmail.com

6/3/2007 8:42:00 PM

0

On 3 jun, 13:59, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 03.06.2007 17:57, maikonara...@gmail.com wrote:
>
> > 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

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


Dan Teitsort

1/11/2008 1:46:00 AM

0

maikonaraujo@gmail.com wrote:
>
> 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'
--
Posted via http://www.ruby-....