I think what you have duplicating functionality---you're not taking advantage of the DeferredSemaphore. Try something like this:<div><br></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>sem = DeferredSemaphore(1)</div>
<div><br></div><div>def doSerialStuffAndRelease(sem):</div><div> # ...perform serial port communication...</div><div> sem.release()<br><div><br></div><div>d = sem.acquire()</div><div>d.addCallback(doSerialStuffAndRelease)</div>
<div><br></div><div>You'll want "sem" to be a global---use wherever you deal with a particular port. Create additional instances of DeferredSemaphore for additional serial ports. Note that when the deferred is fired, the semaphore is passed as the argument. This is the reason for "sem" being the argument name in the function.</div>
<div><br></div><div><a href="http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.2.0/twisted/internet/defer.py#L1266">http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.2.0/twisted/internet/defer.py#L1266</a></div>
<div><br></div><div>Note that you'll also want an error handler which releases the semaphore :)</div><div><br></div><div>Jason<br><br><div class="gmail_quote">On Mon, Feb 7, 2011 at 8:23 PM, Jason Heeris <span dir="ltr"><<a href="mailto:jason.heeris@gmail.com">jason.heeris@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I'm using a DeferredSemaphore with a token count of 1 to control<br>
access to a serial port. I also have a GTK object for which I'd like<br>
the "in-use" property to change (and notify listeners) when the<br>
resource is in use. So far, I have something like this:<br>
<br>
----<br>
class SerialResource(gobject):<br>
<br>
# Property defs, etc<br>
<br>
def _acquire(self):<br>
# The "in-use" property is actually stored in self.taken<br>
self.taken = True<br>
self.notify('in-use')<br>
<br>
def _release(self, res):<br>
self.taken = False<br>
self.notify('in-use')<br>
return res<br>
<br>
def run(self, func, *args, **kargs):<br>
def wrapper():<br>
self._acquire()<br>
res = func(*args, **kargs)<br>
return res<br>
<br>
d = self.sem.run(wrapper)<br>
d.addBoth(self._release)<br>
return d<br>
----<br>
<br>
I feel like there might be a simpler way to do this, but I just can't<br>
see it. Or is this as simple as I can make it?<br>
<br>
— Jason<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Jason Rennie<br>Research Scientist, ITA Software<br>617-714-2645<br><a href="http://www.itasoftware.com/">http://www.itasoftware.com/</a><br><br>
</div></div>