[Stackless] API suggestions
Sune Kirkeby
sune at mel.interspace.dk
Wed Mar 26 12:41:33 CET 2003
[ Oren Tirosh ]
> 1. Represent each channel with two separate objects - one for the
> sender and one of the receiver side. The reference counts of the
> two objects are tracked separately. This can be useful for debugging:
> a tasklet will not block on sending to a channel with no receivers
> or attempting to receive from a channel with no senders. Instead
> an exception will be raised immediately.
Something like the attached? (It does not expose other channel
methods or attributes than send/send_exception/receive, but that
should be an easy).
--
Sune Kirkeby | "Never be afraid to tell the world who you are."
| -- Anonymous
-------------- next part --------------
import stackless, weakref
class ChannelWrapper:
__slots__ = ['real', 'sender', 'receiver']
def __init__(self, channel):
self.real = channel
class SendingChannel:
__slots__ = ['channel']
def __init__(self, channel):
self.channel = channel
self.channel.sender = weakref.ref(self)
def send(self, *args):
assert not self.channel.receiver() is None
return apply(self.channel.real.send, args)
def send_exception(self, *args):
assert not self.channel.receiver() is None
return apply(self.channel.real.send_exception, args)
class ReceivingChannel:
__slots__ = ['channel']
def __init__(self, channel):
self.channel = channel
self.channel.receiver = weakref.ref(self)
def receive(self):
assert not self.channel.sender() is None
return self.channel.real.receive()
def dualchannel():
channel = ChannelWrapper(stackless.channel())
return SendingChannel(channel), ReceivingChannel(channel)
if __name__ == '__main__':
print 'Testing . . .'
s, r = dualchannel()
def f(s):
stackless.tasklet().become()
s.send(42)
f(s)
assert r.receive() == 42
print 'send / receive seems ok.'
try:
s, r = dualchannel()
del s
r.receive()
except AssertionError:
print 'receive seems ok.'
else:
print 'receive seems borked.'
try:
s, r = dualchannel()
del r
s.send(42)
except AssertionError:
print 'send seems ok.'
else:
print 'send seems borked.'
More information about the Stackless
mailing list