I know that if I defer a function and pass some args to it, the function has those args and can work with them, but can a function that's part of an instanced object (for example) access the variables of its object?
class foo (object):
def __init__ (self):
self.bar = 42
def do_work (self):
self.bar += 1
baz = foo()
deferred.defer(baz.do_work)
Would I basically have to give the function all information it would need as arguments?
Also, would baz be trashed if the only reference to it were in a deferred function?
If you pass in an instance method, as you are in your code sample, the entire instance will be serialized and passed around. Any objects that your
foo
object refers to will also be serialized, and so forth. Any global state - for example, module level and class level variables - will not be preserved, so it'll be in whatever state those variables are on the instance the task executes on.The necessary data is serialized and sent off to the task queue at the time you call
defer
, so if that was your only reference to baz, that copy of baz will be immediately garbage collected. That won't stop the deferred task from running, though, as it'll create a new instance when it deserializes and executes the task.