drop custom threadlocal implementation
This commit is contained in:
parent
00a01d2522
commit
ad81bf3f61
2 changed files with 10 additions and 58 deletions
|
|
@ -17,67 +17,21 @@
|
||||||
# Authors:
|
# Authors:
|
||||||
# Mike McLean <mikem@redhat.com>
|
# Mike McLean <mikem@redhat.com>
|
||||||
|
|
||||||
# This modules provides a thread-safe way of passing
|
# This module provides a threadlocal instance that kojihub uses to store
|
||||||
# request context around in a global way
|
# request context.
|
||||||
# - db connections
|
# In the past, we had a custom ThreadLocal implementation, but now this is
|
||||||
# - request data
|
# just a thin wrapper around threading.local
|
||||||
# - auth data
|
|
||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import six
|
import threading
|
||||||
import six.moves._thread
|
|
||||||
|
|
||||||
|
|
||||||
class _data(object):
|
class ThreadLocal(threading.local):
|
||||||
pass
|
"""A small compatibility wrapper around threading.local"""
|
||||||
|
|
||||||
|
|
||||||
class ThreadLocal(object):
|
|
||||||
def __init__(self):
|
|
||||||
object.__setattr__(self, '_tdict', {})
|
|
||||||
|
|
||||||
# should probably be getattribute, but easier to debug this way
|
|
||||||
def __getattr__(self, key):
|
|
||||||
id = six.moves._thread.get_ident()
|
|
||||||
tdict = object.__getattribute__(self, '_tdict')
|
|
||||||
if id not in tdict:
|
|
||||||
raise AttributeError(key)
|
|
||||||
data = tdict[id]
|
|
||||||
return object.__getattribute__(data, key)
|
|
||||||
|
|
||||||
def __setattr__(self, key, value):
|
|
||||||
id = six.moves._thread.get_ident()
|
|
||||||
tdict = object.__getattribute__(self, '_tdict')
|
|
||||||
if id not in tdict:
|
|
||||||
tdict[id] = _data()
|
|
||||||
data = tdict[id]
|
|
||||||
return object.__setattr__(data, key, value)
|
|
||||||
|
|
||||||
def __delattr__(self, key):
|
|
||||||
id = six.moves._thread.get_ident()
|
|
||||||
tdict = object.__getattribute__(self, '_tdict')
|
|
||||||
if id not in tdict:
|
|
||||||
raise AttributeError(key)
|
|
||||||
data = tdict[id]
|
|
||||||
ret = object.__delattr__(data, key)
|
|
||||||
if len(data.__dict__) == 0:
|
|
||||||
del tdict[id]
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
id = six.moves._thread.get_ident()
|
|
||||||
tdict = object.__getattribute__(self, '_tdict')
|
|
||||||
return "(current thread: %s) {" % id + \
|
|
||||||
", ".join(["%s : %s" % (k, v.__dict__) for (k, v) in six.iteritems(tdict)]) + \
|
|
||||||
"}"
|
|
||||||
|
|
||||||
def _threadclear(self):
|
def _threadclear(self):
|
||||||
id = six.moves._thread.get_ident()
|
self.__dict__.clear()
|
||||||
tdict = object.__getattribute__(self, '_tdict')
|
|
||||||
if id not in tdict:
|
|
||||||
return
|
|
||||||
del tdict[id]
|
|
||||||
|
|
||||||
|
|
||||||
context = ThreadLocal()
|
context = ThreadLocal()
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ import os
|
||||||
# del psycopg2.extensions.string_types[1266]
|
# del psycopg2.extensions.string_types[1266]
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
|
@ -56,10 +57,7 @@ _DBopts = None
|
||||||
# Apache forks a new worker, and that connection
|
# Apache forks a new worker, and that connection
|
||||||
# will be used to service all requests handled
|
# will be used to service all requests handled
|
||||||
# by that worker.
|
# by that worker.
|
||||||
# This probably doesn't need to be a ThreadLocal
|
_DBconn = threading.local()
|
||||||
# since Apache is not using threading,
|
|
||||||
# but play it safe anyway.
|
|
||||||
_DBconn = koji.context.ThreadLocal()
|
|
||||||
|
|
||||||
logger = logging.getLogger('koji.db')
|
logger = logging.getLogger('koji.db')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue