[mod_python] Best way to have an unique_id per request

Graham Dumpleton graham.dumpleton at gmail.com
Thu Apr 24 03:29:10 EDT 2008


2008/4/24 Luca Montecchiani <l.montecchiani at teamsystem.com>:
> Hi  Graham,
>  so you implicit suggest to avoid  "Session._new_sid(req)" ?

Don't know, just wasn't my first choice.

>  I've already used req.connection.id and instead of req.connection.remote_ip
>  I've used the req.connection.remote_addr because there is also the
>  client port but I've noticed that this three values doesn't change
>  if I repeatedly refresh the page :( probably the tcp client stack cache the
> last free
>  used port for an immediate reuse... on windows I've to wait at least 6
> seconds
>  between a refresh to have different values :(
>
>  def _getunique(req):
>    import os
>    import thread
>
>    req.uniqidprog = req.uniqidprog + 1
>    _tmpid = "%d:%d:%s:%s:%d:%s:%d" % ( os.getpid(),
>                                     thread.get_ident(),
>                                     req.connection.remote_addr[0],
>                                     req.connection.remote_addr[1],
>                                     req.connection.id,
>                                     req.request_time,
>                                     req.uniqidprog )
>
>    return _tmpid
>  Any other suggestion ?

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

The req.connection.id would stay the same if keep alive is used and
connection maintained.

Graham


More information about the Mod_python mailing list