Mercurial > cosocket
changeset 22:425776983cf2
Removed the weakref mechanism from channels.py and instead turned until_message_received() into a generator rather than an object. Added a return_value() instruction to cosocket.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 19 Apr 2009 23:06:30 -0700 |
parents | b77e679ce993 |
children | 1683a8fc76b0 |
files | channels.py cosocket.py |
diffstat | 2 files changed, 38 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/channels.py Sun Apr 19 22:16:11 2009 -0700 +++ b/channels.py Sun Apr 19 23:06:30 2009 -0700 @@ -1,4 +1,4 @@ -import weakref +import cosocket _channels = {} @@ -9,19 +9,33 @@ def execute(self, dispatcher): if self.channel_name in _channels: - for weakling in _channels[self.channel_name].itervaluerefs(): - receiver = weakling() - if receiver: - receiver.continue_from_yield(self.message) + receivers = _channels[self.channel_name].values() del _channels[self.channel_name] + for receiver in receivers: + receiver.continue_from_yield(self.message) dispatcher.continue_from_yield() -class until_message_received(object): +class _until_message_received(object): def __init__(self, channel_name): self.channel_name = channel_name + self._fd = None def execute(self, dispatcher): if self.channel_name not in _channels: - _channels[self.channel_name] = weakref.WeakValueDictionary() - fd = dispatcher.socket.fileno() - _channels[self.channel_name][fd] = dispatcher + _channels[self.channel_name] = {} + self._fd = dispatcher.socket.fileno() + _channels[self.channel_name][self._fd] = dispatcher + + def abort(self): + if (self.channel_name in _channels and + self._fd in _channels[self.channel_name]): + del _channels[self.channel_name][self._fd] + +def until_message_received(channel_name): + instruction = _until_message_received(channel_name) + try: + message = yield instruction + yield cosocket.return_value(message) + except GeneratorExit: + instruction.abort() + raise
--- a/cosocket.py Sun Apr 19 22:16:11 2009 -0700 +++ b/cosocket.py Sun Apr 19 23:06:30 2009 -0700 @@ -14,6 +14,14 @@ if conn: self.continue_from_yield() + def close_coroutine_and_return_to_caller(self, message): + self.__close_coroutine(self.__coroutine) + if self.__coroutine_stack: + self.__coroutine = self.__coroutine_stack.pop() + self.continue_from_yield(message) + else: + self.__coroutine = None + def continue_from_yield(self, message = None, exception = None): try: if exception: @@ -131,3 +139,10 @@ def execute(self, dispatcher): dispatcher.push(self.content) + +class return_value(object): + def __init__(self, value): + self.value = value + + def execute(self, dispatcher): + dispatcher.close_coroutine_and_return_to_caller(self.value)