@@ -35,11 +35,42 @@ class StackInABox(object):
3535
3636 """
3737
38+ @classmethod
39+ def get_thread_instance (cls ):
40+ """
41+ Interface to the thread storage to ensure the instance properly exists
42+ """
43+ create = False
44+
45+ # if the `instance` property doesn't exist
46+ if not hasattr (local_store , 'instance' ):
47+ local_store .instance = None
48+ create = True
49+
50+ # if the instance doesn't exist at all
51+ elif local_store .instance is None :
52+ create = True
53+
54+ # if it's something else entirely...
55+ elif not isinstance (local_store .instance , cls ):
56+ local_store .instance = None
57+ create = True
58+
59+ # if the above conditions are met, create it
60+ if create :
61+ logger .debug ('Creating new StackInABox instance...' )
62+ local_store .instance = cls ()
63+ logger .debug (
64+ 'Created StackInABox({0})' .format (local_store .instance .__id )
65+ )
66+
67+ return local_store .instance
68+
3869 @classmethod
3970 def reset_services (cls ):
4071 """Reset the thread's StackInABox instance."""
4172 logger .debug ('Resetting services' )
42- return local_store . instance .reset ()
73+ return cls . get_thread_instance () .reset ()
4374
4475 @classmethod
4576 def register_service (cls , service ):
@@ -51,7 +82,7 @@ def register_service(cls, service):
5182
5283 """
5384 logger .debug ('Registering service {0}' .format (service .name ))
54- return local_store . instance .register (service )
85+ return cls . get_thread_instance () .register (service )
5586
5687 @classmethod
5788 def call_into (cls , method , request , uri , headers ):
@@ -66,7 +97,7 @@ def call_into(cls, method, request, uri, headers):
6697
6798 """
6899 logger .debug ('Request: {0} - {1}' .format (method , uri ))
69- return local_store . instance .call (method ,
100+ return cls . get_thread_instance () .call (method ,
70101 request ,
71102 uri ,
72103 headers )
@@ -85,7 +116,7 @@ def hold_onto(cls, name, obj):
85116 """
86117 logger .debug ('Holding on {0} of type {1} with id {2}'
87118 .format (name , type (obj ), id (obj )))
88- local_store . instance .into_hold (name , obj )
119+ cls . get_thread_instance () .into_hold (name , obj )
89120
90121 @classmethod
91122 def hold_out (cls , name ):
@@ -102,7 +133,7 @@ def hold_out(cls, name):
102133 """
103134 logger .debug ('Retreiving {0} from hold'
104135 .format (name ))
105- obj = local_store . instance .from_hold (name )
136+ obj = cls . get_thread_instance () .from_hold (name )
106137 logger .debug ('Retrieved {0} of type {1} with id {2} from hold'
107138 .format (name , type (obj ), id (obj )))
108139 return obj
@@ -115,7 +146,7 @@ def update_uri(cls, uri):
115146
116147 """
117148 logger .debug ('Request: Update URI to {0}' .format (uri ))
118- local_store . instance .base_url = uri
149+ cls . get_thread_instance () .base_url = uri
119150
120151 def __init__ (self ):
121152 """Initialize the StackInABox instance.
@@ -307,4 +338,3 @@ def from_hold(self, name):
307338
308339# Thread local instance of StackInABox
309340local_store = threading .local ()
310- local_store .instance = StackInABox ()
0 commit comments