[mod_python] Best way to have an unique_id per request

Graham Dumpleton graham.dumpleton at gmail.com
Thu Apr 24 06:10:59 EDT 2008


2008/4/24 Luca Montecchiani <l.montecchiani at teamsystem.com>:
> Graham Dumpleton ha scritto:
>
>
>
> > Don't stick uniqidprog in req, it will always be 0 as a result. It
> > needs to be a global module variable which is thread protected.
> >
> >  from threading import Lock
> >  lock = Lock()
> >  uniqidprog = 1
> >
> >  def _getunique(req):
> >    lock.acquire()
> >    value = uniqidprog
> >    uniqidprog += 1
> >    lock.release()
> >
> >    ... use
> >
> >
>
>  In my own publisher I've many req.somevarname = somevalue
>  without worrying about locking simply because the docs suggest it :
>
>  "4.5.4 Request Object
>  The request object is a Python mapping to the Apache request rec structure.
> When a handler is invoked, it is
>  always passed a single argument - the request object.
>  You can dynamically assign attributes to it as a way to communicate between
> handlers."

Yes, but in this case you want the counter to be common to all request
handlers executing in that process over time, otherwise it is
pointless having it. When you assign to req object, it only exists for
that one request.

Maybe I am misunderstanding what you are wanting to do. :-)

Graham


More information about the Mod_python mailing list