@@ -56,40 +56,56 @@ def wrapper(wrapped, instance, args, kwargs):
56
56
return wrapper
57
57
58
58
59
- @wrapt .decorator
60
- def defer_entity_syncing (wrapped , instance , args , kwargs ):
59
+ def defer_entity_syncing (* args , handler = None ):
61
60
"""
62
- A decorator that can be used to defer the syncing of entities until after the method has been run
63
- This is being introduced to help avoid deadlocks in the meantime as we attempt to better understand
64
- why they are happening
61
+ A decorator for deferring entity syncing until after the function is complete
62
+ An optional handler can be specified to handle the entity syncing.
63
+ If no handler is passed the default sync_entities method will be called
65
64
"""
66
65
67
- # Defer entity syncing while we run our method
68
- sync_entities . defer = True
66
+ # Set a default handler
67
+ handler = handler or sync_entities
69
68
70
- # Run the method
71
- try :
72
- return wrapped (* args , ** kwargs )
69
+ @wrapt .decorator
70
+ def wrapper (wrapped , instance , args , kwargs ):
71
+ """
72
+ A decorator that can be used to defer the syncing of entities until after the method has been run
73
+ This is being introduced to help avoid deadlocks in the meantime as we attempt to better understand
74
+ why they are happening
75
+ """
73
76
74
- # After we run the method disable the deferred syncing
75
- # and sync all the entities that have been buffered to be synced
76
- finally :
77
- # Enable entity syncing again
78
- sync_entities .defer = False
77
+ # Defer entity syncing while we run our method
78
+ sync_entities .defer = True
79
79
80
- # Get the models that need to be synced
81
- model_objs = list (sync_entities .buffer .values ())
80
+ # Run the method
81
+ try :
82
+ return wrapped (* args , ** kwargs )
82
83
83
- # If none is in the model objects we need to sync all
84
- if None in sync_entities .buffer :
85
- model_objs = list ()
84
+ # After we run the method disable the deferred syncing
85
+ # and sync all the entities that have been buffered to be synced
86
+ finally :
87
+ # Enable entity syncing again
88
+ sync_entities .defer = False
86
89
87
- # Sync the entities that were deferred if any
88
- if len (sync_entities .buffer ):
89
- sync_entities (* model_objs )
90
+ # Get the models that need to be synced
91
+ model_objs = list (sync_entities .buffer .values ())
92
+
93
+ # If none is in the model objects we need to sync all
94
+ if None in sync_entities .buffer :
95
+ model_objs = list ()
90
96
91
- # Clear the buffer
92
- sync_entities .buffer = {}
97
+ # Sync the entities that were deferred if any
98
+ if len (sync_entities .buffer ):
99
+ handler (* model_objs )
100
+
101
+ # Clear the buffer
102
+ sync_entities .buffer = {}
103
+
104
+ # If the decorator is called without arguments
105
+ if len (args ) == 1 and callable (args [0 ]):
106
+ return wrapper (args [0 ])
107
+ else :
108
+ return wrapper
93
109
94
110
95
111
@wrapt .decorator
@@ -193,6 +209,7 @@ def sync_entities(*model_objs):
193
209
Args:
194
210
model_objs (List[Model]): The model objects to sync. If empty, all entities will be synced
195
211
"""
212
+
196
213
if sync_entities .suppress :
197
214
# Return false that we did not do anything
198
215
return False
0 commit comments