On 02-10-2008, Graham Dumpleton wrote:
>
> 2008/10/3 William Dode <[EMAIL PROTECTED]>:
>>
>> Hi,
>>
>> In daemon mode, with threads=1 :
>>
>> def application(environ, start_response):
>>
>>    status = '200 OK'
>>    output = "con= %s" % cgi.escape(repr(pool.connection()))
>>    loc = threading.local()
>>    try:
>>        loc.i += 1
>>    except AttributeError:
>>        loc.i = 0
>>    output = 'i=%s' % loc.i
>>
>>    response_headers = [('Content-type', 'text/html'), ]
>>    start_response(status, response_headers)
>>
>>    return [output]
>>
>> threading.local doesn't stay between requests. Is it normal ?
>>
>> I found this with DBUtils wich doesn't reuse connection.
>>
>> I missed something ?
>
> If you want something to persist between requests, just make it a
> global variable within the file, you do not need to use
> threading.local(). Depending on type of data, global declaration may
> be needed in function scope so that on assignment it doesn't just
> create a local variable to function (which is what is happening now).

I thought it's a singleton...

But even if i put it outside the function it doesn't show what i expect 
:

import cgi
import threading
loc = threading.local()

def application(environ, start_response):

    status = '200 OK'
    try:
        loc.i += 1
    except AttributeError:
        loc.i = 0
    output = '%s i=%s' % (loc, loc.i)

    response_headers = [('Content-type', 'text/html'), ]
    start_response(status, response_headers)

    return [cgi.escape(output)]

The output is everytime <thread._local object at 0xb710d6e0> i=0

>
> In doing this, if a multithreaded application you would however need
> to protect using thread mutex locks any updates to the global data.
> This is presuming all threads should share the global data. If each
> thread should have its own, then threading.local() may be okay, but
> you need to initialise it once outside of the function, actually at
> global scope, not like what you are doing now.

It seems that DBUtils.PersistentDB use threading.local() to keep one 
connection for each thread. I cannot make it work with mod_wsgi...

>
> BTW, also be careful of embedded mode as source code reloading could
> cause issues in that case. See:
>
>   http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

I use only deamon mode

thx

-- 
William Dodé - http://flibuste.net
Informaticien Indépendant


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/modwsgi?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to