Use a more generic threading local monkey patch

This commit is contained in:
donovan
2008-06-03 12:40:13 -07:00
parent 443cacf345
commit 1637ce726c

View File

@@ -26,6 +26,7 @@ THE SOFTWARE.
import os import os
import select import select
import socket import socket
import sys
import errno import errno
try: try:
@@ -118,7 +119,6 @@ def wrap_socket_with_coroutine_socket():
socket.getaddrinfo = new_getaddrinfo socket.getaddrinfo = new_getaddrinfo
def new_fromfd(*args, **kw): def new_fromfd(*args, **kw):
print "fromfd", args, kw
from eventlet import greenio from eventlet import greenio
return greenio.GreenSocket(__original_fromfd__(*args, **kw)) return greenio.GreenSocket(__original_fromfd__(*args, **kw))
socket.fromfd = new_fromfd socket.fromfd = new_fromfd
@@ -209,36 +209,37 @@ def wrap_select_with_coroutine_select():
select.select = fake_select select.select = fake_select
def wrap_paste_tls_with_corols(): try:
"""Paste uses threadlocals, in a module called paste.util.threadinglocal. import threading
If you are running Paste under an eventlet web server that uses greenlets __original_threadlocal__ = threading.local
instead of threads, you can use wrap_paste_tls_with_corols to replace except ImportError:
the paste.util.threadinglocal.local class with one that uses the current pass
greenlet id as the 'thread' id instead of the current thread.
def wrap_threading_local_with_coro_local():
"""monkey patch threading.local with something that is
greenlet aware. Since greenlets cannot cross threads,
so this should be semantically identical to threadlocal.local
""" """
from eventlet import api from eventlet import api
from paste.util import threadinglocal
def get_ident(): def get_ident():
return id(api.getcurrent()) return id(api.getcurrent())
class local(object): class local(object):
def __init__(self): def __init__(self):
self.__dict__['__objs'] = {} self.__dict__['__objs'] = {}
def __getattr__(self, attr, g=get_ident): def __getattr__(self, attr, g=get_ident):
print "getattr", self, attr, g
try: try:
return self.__dict__['__objs'][g()][attr] return self.__dict__['__objs'][g()][attr]
except KeyError: except KeyError:
raise AttributeError( raise AttributeError(
"No variable %s defined for the thread %s" "No variable %s defined for the thread %s"
% (attr, g())) % (attr, g()))
def __setattr__(self, attr, value, g=get_ident): def __setattr__(self, attr, value, g=get_ident):
self.__dict__['__objs'].setdefault(g(), {})[attr] = value self.__dict__['__objs'].setdefault(g(), {})[attr] = value
def __delattr__(self, attr, g=get_ident): def __delattr__(self, attr, g=get_ident):
try: try:
del self.__dict__['__objs'][g()][attr] del self.__dict__['__objs'][g()][attr]
@@ -247,7 +248,7 @@ def wrap_paste_tls_with_corols():
"No variable %s defined for thread %s" "No variable %s defined for thread %s"
% (attr, g())) % (attr, g()))
threadinglocal.local = local threading.local = local
def socket_bind_and_listen(descriptor, addr=('', 0), backlog=50): def socket_bind_and_listen(descriptor, addr=('', 0), backlog=50):