[Twisted-Python] [PATCH] Multiple Reactors

Moshe Zadka m at moshez.org
Sun May 11 17:12:59 MDT 2003


Hi!
The following patch removes the globals from SelectReactor, so
that multiple SelectReactors are possible.

Index: twisted/internet/default.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/default.py,v
retrieving revision 1.79
diff -u -r1.79 default.py
--- twisted/internet/default.py	10 May 2003 12:52:37 -0000	1.79
+++ twisted/internet/default.py	11 May 2003 23:11:26 -0000
@@ -367,8 +367,6 @@
 
 
 # global state for selector
-reads = {}
-writes = {}
 
 
 def win32select(r, w, e, timeout=None):
@@ -403,13 +401,18 @@
 
     __implements__ = (PosixReactorBase.__implements__, IReactorFDSet)
 
+    def __init__(self):
+        PosixReactorBase.__init__(self)
+        self.reads = {}
+        self.writes = {}
+
     def _preenDescriptors(self):
         log.msg("Malformed file descriptor found.  Preening lists.")
-        readers = reads.keys()
-        writers = writes.keys()
-        reads.clear()
-        writes.clear()
-        for selDict, selList in ((reads, readers), (writes, writers)):
+        readers = self.reads.keys()
+        writers = self.writes.keys()
+        self.reads.clear()
+        self.writes.clear()
+        for selDict, selList in ((self.reads, readers), (self.writes, writers)):
             for selectable in selList:
                 try:
                     select.select([selectable], [selectable], [selectable], 0)
@@ -419,19 +422,19 @@
                     selDict[selectable] = 1
 
 
-    def doSelect(self, timeout,
-                 # Since this loop should really be as fast as possible,
-                 # I'm caching these global attributes so the interpreter
-                 # will hit them in the local namespace.
-                 reads=reads,
-                 writes=writes,
-                 rhk=reads.has_key,
-                 whk=writes.has_key):
+    def doSelect(self, timeout):
         """Run one iteration of the I/O monitor loop.
 
         This will run all selectables who had input or output readiness
         waiting for them.
         """
+        # Since this loop should really be as fast as possible,
+        # I'm caching these global attributes so the interpreter
+        # will hit them in the local namespace.
+        reads=self.reads
+        writes=self.writes
+        rhk=reads.has_key
+        whk=writes.has_key
         while 1:
             try:
                 r, w, ignored = _select(reads.keys(),
@@ -502,33 +505,33 @@
     def addReader(self, reader):
         """Add a FileDescriptor for notification of data available to read.
         """
-        reads[reader] = 1
+        self.reads[reader] = 1
 
     def addWriter(self, writer):
         """Add a FileDescriptor for notification of data available to write.
         """
-        writes[writer] = 1
+        self.writes[writer] = 1
 
     def removeReader(self, reader):
         """Remove a Selectable for notification of data available to read.
         """
-        if reads.has_key(reader):
-            del reads[reader]
+        if self.reads.has_key(reader):
+            del self.reads[reader]
 
     def removeWriter(self, writer):
         """Remove a Selectable for notification of data available to write.
         """
-        if writes.has_key(writer):
-            del writes[writer]
+        if self.writes.has_key(writer):
+            del self.writes[writer]
 
     def removeAll(self):
         """Remove all readers and writers, and return list of Selectables."""
-        readers = reads.keys()
+        readers = self.reads.keys()
         for reader in readers:
-            if reads.has_key(reader):
-                del reads[reader]
-            if writes.has_key(reader):
-                del writes[reader]
+            if self.reads.has_key(reader):
+                del self.reads[reader]
+            if self.writes.has_key(reader):
+                del self.writes[reader]
         return readers
 
 

-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/




More information about the Twisted-Python mailing list