On Aug 17, 2011, at 10:14 AM, Carlos wrote:

> What would be the best way to extend (add methods, not subclass) web2py 
> classes?.
> 
> For reference if I want to add the method 'test' to db (DAL class)?.
> 
> I was doing something automated, but basically the following as a specific 
> example:
> 
>    def test(self): return 'test from DAL'
>    import gluon
>    setattr(gluon.dal.DAL, 'test', test)
> 
> And that works ok so far:
> 
>    db.test()  >  'test from DAL'
> 
> But I believe that is NOT thread / concurrent safe, right?.
> 
> I need to add multiple methods to multiple web2py classes (the above is just 
> a simple example to illustrate my goal).
> 
> And I also need to have access to the environment's own variables (db, 
> request, response, my own instances, etc.) on each web request, in a *safe* 
> way (without 'mixing' anything).
> 
> Is there anything you could recommend in order to accomplish this?.
> 
> I believe this has something (or everything) to do with the concurrency 
> problems I describe in my other post:
> 
>    https://groups.google.com/d/topic/web2py/3qIJXnGV-mc/discussion
> 

One possible problem with your approach is that the method 'test' is going to 
disappear at the end of your request. And if you have multiple outstanding 
requests, this could be a problem. Request A patches 'test', then request B 
patches 'test', then request B terminates, and finally request A calls 'test' 
and gets the now-orphaned B reference.

Why *not* subclass instead? The subclass definition would be private to each 
request?


Reply via email to