[Twisted-Python] problems with callback in an iteration

Justin Johnson justinjohnson at fastmail.fm
Wed Jul 30 13:44:14 MDT 2003


In the code below, I create a Trigger object (something I defined) and
call the "make" method on it.  Then I add a callback so that I can "lock"
it after it has been made.  I'm getting some wierd results though.  The
output from the first print statement...

print "trigger name", self.name

...prints the names of the triggers correctly, as they appear in the list
(trigger1 followed by trigger2).  However, the callback to _lock is only
called with the last element in the list (trigger2), but it is called
twice.  So instead of calling _lock on trigger1 and then on trigger2, it
is called twice on trigger2.

Does anyone see what I'm doing wrong?

[snip]
def applyTriggers(self):
	"""Apply the standard triggers to the VOB.
	"""
	deferreds = []
	trigger1 = trigger.PreRmverTrigger("pre_rmver","Prevent removing of versions",config.thisServer,self.tag)
	trigger2 = trigger.PreRmelemTrigger("pre_rmelem","Prevent removing of elements",config.thisServer,self.tag)

	for t in [trigger1, trigger2]:
		print "trigger name:", t.name

		def _lock(results):
			print "*** locking trigger:", t.name
			d = t.lock()
			return d

		d = t.make()
		d.addCallbacks(_lock, log.err)
		deferreds.append(d)
	return defer.DeferredList(deferreds)
[/snip]




More information about the Twisted-Python mailing list