mailr17806 - in /trunk: compat.py gui/controller.py gui/interpreter.py status.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on October 15, 2012 - 14:25:
Author: bugman
Date: Mon Oct 15 14:25:51 2012
New Revision: 17806

URL: http://svn.gna.org/viewcvs/relax?rev=17806&view=rev
Log:
Python 2.4 and earlier fixes for the Queuing module.

The TaskQueue class from http://code.activestate.com/recipes/475160/ which 
was added to Python 2.5+
has been added to the compat module.  This module is now used for all imports 
of the Queue class for
all Python versions.


Modified:
    trunk/compat.py
    trunk/gui/controller.py
    trunk/gui/interpreter.py
    trunk/status.py

Modified: trunk/compat.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/compat.py?rev=17806&r1=17805&r2=17806&view=diff
==============================================================================
--- trunk/compat.py (original)
+++ trunk/compat.py Mon Oct 15 14:25:51 2012
@@ -23,10 +23,15 @@
 """Temporary module for allowing relax to support both Python 2 and 3."""
 
 # Python module imports.
+import sys
 from copy import deepcopy
 import os
 import platform
-import sys
+if sys.version_info[0] == 2:
+    from Queue import Queue as Queue2
+else:
+    from queue import Queue as Queue3
+import threading
 
 
 # The operating system.
@@ -44,6 +49,73 @@
 
     # Return the new data.
     return new_data
+
+
+
+class TaskQueue(Queue2):
+    """Python 2.4 and earlier Queuing class replacement.
+
+    This code comes from http://code.activestate.com/recipes/475160/ and is 
part of the Python sources from 2.5 onwards.
+    """
+
+    def __init__(self):
+        Queue2.__init__(self)        
+        self.all_tasks_done = threading.Condition(self.mutex)
+        self.unfinished_tasks = 0
+
+    def _put(self, item):
+        Queue2._put(self, item)
+        self.unfinished_tasks += 1        
+
+    def task_done(self):
+        """Indicate that a formerly enqueued task is complete.
+
+        Used by Queue consumer threads.  For each get() used to fetch a task,
+        a subsequent call to task_done() tells the queue that the processing
+        on the task is complete.
+
+        If a join() is currently blocking, it will resume when all items
+        have been processed (meaning that a task_done() call was received
+        for every item that had been put() into the queue).
+
+        Raises a ValueError if called more times than there were items
+        placed in the queue.
+        """
+        self.all_tasks_done.acquire()
+        try:
+            unfinished = self.unfinished_tasks - 1
+            if unfinished <= 0:
+                if unfinished < 0:
+                    raise ValueError('task_done() called too many times')
+                self.all_tasks_done.notifyAll()
+            self.unfinished_tasks = unfinished
+        finally:
+            self.all_tasks_done.release()
+
+    def join(self):
+        """Blocks until all items in the Queue have been gotten and 
processed.
+
+        The count of unfinished tasks goes up whenever an item is added to 
the
+        queue. The count goes down whenever a consumer thread calls 
task_done()
+        to indicate the item was retrieved and all work on it is complete.
+
+        When the count of unfinished tasks drops to zero, join() unblocks.
+        """
+        self.all_tasks_done.acquire()
+        try:
+            while self.unfinished_tasks:
+                self.all_tasks_done.wait()
+        finally:
+            self.all_tasks_done.release()
+
+
+# Alias the correct Queue.
+if sys.version_info[0] == 2 and sys.version_info[1] <= 4:
+    Queue = TaskQueue    # Alias the TaskQueue for Python 2.4 and earlier.
+elif sys.version_info[0] == 2:
+    Queue = Queue2
+else:
+    Queue = Queue3
 
 
 # The Python version.

Modified: trunk/gui/controller.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/gui/controller.py?rev=17806&r1=17805&r2=17806&view=diff
==============================================================================
--- trunk/gui/controller.py (original)
+++ trunk/gui/controller.py Mon Oct 15 14:25:51 2012
@@ -24,15 +24,12 @@
 """Log window of relax GUI controlling all calculations."""
 
 # Python module imports.
-try:
-    from queue import Queue
-except ImportError:
-    from Queue import Queue
 import sys
 import wx
 import wx.stc
 
 # relax module imports.
+from compat import Queue
 from generic_fns.pipes import cdp_name
 from relax_io import SplitIO
 from status import Status; status = Status()

Modified: trunk/gui/interpreter.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/gui/interpreter.py?rev=17806&r1=17805&r2=17806&view=diff
==============================================================================
--- trunk/gui/interpreter.py (original)
+++ trunk/gui/interpreter.py Mon Oct 15 14:25:51 2012
@@ -23,10 +23,6 @@
 """A threaded version of the relax interpreter for use by the GUI."""
 
 # Python module imports.
-try:
-    from queue import Queue
-except ImportError:
-    from Queue import Queue
 from re import search
 import sys
 from threading import Thread
@@ -35,6 +31,7 @@
 import wx
 
 # relax module imports.
+from compat import Queue
 from prompt import interpreter
 from relax_errors import AllRelaxErrors
 from status import Status; status = Status()

Modified: trunk/status.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/status.py?rev=17806&r1=17805&r2=17806&view=diff
==============================================================================
--- trunk/status.py (original)
+++ trunk/status.py Mon Oct 15 14:25:51 2012
@@ -26,15 +26,12 @@
 from os import F_OK, access
 from os.path import sep
 import platform
-try:
-    from queue import Queue
-except ImportError:
-    from Queue import Queue
 from re import search
 import sys
 from threading import Lock, RLock
 
 # relax module imports.
+from compat import Queue
 from relax_errors import RelaxError
 
 




Related Messages


Powered by MHonArc, Updated Mon Oct 15 15:00:02 2012