Is it safe to instantiate a resource more then once? #2158
-
Looking at the example in suffixed response, when the above question arose. What is the difference between (one instance):
and two-times instantiated resource:
? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi again @mattwarrick! AFAIK, Falcon doesn't make any assumptions about resource instances apart from (when using the default router) just looking for callable attributes that look like responders (e.g., Although it is probably a bit strange to instantiate class Resource:
def __init__(self, database_pool, ...):
...
app.add_route('/cache/objects/{object_id}', Resource(ro_db_replica_pool))
app.add_route('/objects/{object_id}', Resource(rw_db_pool)) |
Beta Was this translation helpful? Give feedback.
Hi again @mattwarrick!
Yes, it is absolutely up to you, the developer, whether it is safe or not.
AFAIK, Falcon doesn't make any assumptions about resource instances apart from (when using the default router) just looking for callable attributes that look like responders (e.g.,
on_get
,on_post
, etc). I suppose you might have some (probably negligible) memory savings reusing the same instance, but the framework doesn't mandate it.Although it is probably a bit strange to instantiate
Calculator()
again in the above example, but in the case a resource's initializer takes parameters, it might be even desirable to create different resources instances from the same class, for instance: