Provide common `fetch_current_thread_functor` function

This code is appearing in various projects (currently
oslo.messaging, taskflow, oslo.concurrency) and it seems
better placed in this little utility helper to avoid
people from duplicating it.

Change-Id: I177d79d4561b3eb13822d3e2ae4a9fbe011f586a
This commit is contained in:
Joshua Harlow 2015-03-16 09:28:21 -07:00 committed by Joshua Harlow
parent 91dc782c2f
commit 1a6dc73254
1 changed files with 18 additions and 0 deletions

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import threading
import warnings
from oslo_utils import importutils
@ -33,6 +34,23 @@ _ALL_PATCH = frozenset(['__builtin__', 'MySQLdb', 'os',
'psycopg', 'select', 'socket', 'thread', 'time'])
def fetch_current_thread_functor():
# Until https://github.com/eventlet/eventlet/issues/172 is resolved
# or addressed we have to use complicated workaround to get a object
# that will not be recycled; the usage of threading.current_thread()
# doesn't appear to currently be monkey patched and therefore isn't
# reliable to use (and breaks badly when used as all threads share
# the same current_thread() object)...
if not EVENTLET_AVAILABLE:
return threading.current_thread
else:
green_threaded = _patcher.is_monkey_patched('thread')
if green_threaded:
return _eventlet.getcurrent
else:
return threading.current_thread
def warn_eventlet_not_patched(expected_patched_modules=None,
what='this library'):
"""Warns if eventlet is being used without patching provided modules.