home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / rb / Coroutine.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  2.2 KB  |  66 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*- 
  2. #
  3. # Copyright (C) 2006 - Ed Catmur <ed@catmur.co.uk>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  11. # GStreamer plugins to be used and distributed together with GStreamer
  12. # and Rhythmbox. This permission is above and beyond the permissions granted
  13. # by the GPL license by which Rhythmbox is covered. If you modify this code
  14. # you may extend this exception to your version of the code, but you are not
  15. # obligated to do so. If you do not wish to do so, delete this exception
  16. # statement from your version.
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program; if not, write to the Free Software
  24. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  25.  
  26. class Coroutine:
  27.     """A simple message-passing coroutine implementation. 
  28.     Not thread- or signal-safe. 
  29.     Usage:
  30.         def my_iter (plexer, args):
  31.             some_async_task (..., callback=plexer.send (tokens))
  32.             yield None
  33.             tokens, (data, ) = plexer.receive ()
  34.             ...
  35.         Coroutine (my_iter, args).begin ()
  36.     """
  37.     def __init__ (self, iter, *args):
  38.         self._continuation = iter (self, *args)
  39.         self._executing = False
  40.     def _resume (self):
  41.         if not self._executing:
  42.             self._executing = True
  43.             try:
  44.                 try:
  45.                     self._continuation.next ()
  46.                     while self._data:
  47.                         self._continuation.next ()
  48.                 except StopIteration:
  49.                     pass
  50.             finally:
  51.                 self._executing = False
  52.     def clear (self):
  53.         self._data = []
  54.     def begin (self):
  55.         self.clear ()
  56.         self._resume ()
  57.     def send (self, *tokens):
  58.         def callback (*args):
  59.             self._data.append ((tokens, args))
  60.             self._resume ()
  61.         return callback
  62.     def receive (self):
  63.         return self._data.pop (0)
  64.  
  65.