[Python-Dev] threadsafe patch for asynchat

Mark Edgington edgimar at lycos.com
Sun Feb 5 22:26:48 CET 2006

Does anyone have any comments about applying the following patch to 
asynchat?  It should not affect the behavior of the module in any way 
for those who do not want to use the feature provided by the patch.  The 
point of the patch is to make it easy to use asynchat in a multithreaded 
application.  Maybe I am missing something, and the patch really doesn't 
make it threadsafe?  Any comments would be appreciated.  Also, if it 
looks good to everyone, feel free to use it.

-------BEGIN PATCH----------
--- asynchat.py    Fri Oct 15 03:03:16 2004
+++ asynchat.py.new    Sun Feb 05 22:05:42 2006
@@ -59,10 +59,11 @@
     ac_in_buffer_size       = 4096
     ac_out_buffer_size      = 4096
-    def __init__ (self, conn=None):
+    def __init__ (self, conn=None, running_in_thread=False):
         self.ac_in_buffer = ''
         self.ac_out_buffer = ''
         self.producer_fifo = fifo()
+        self.running_in_thread = runnning_in_thread
         asyncore.dispatcher.__init__ (self, conn)
     def collect_incoming_data(self, data):
@@ -157,7 +158,9 @@
     def push (self, data):
         self.producer_fifo.push (simple_producer (data))
-        self.initiate_send()
+        # only initiate a send if not running in a threaded 
environment, since
+        # initiate_send() is not threadsafe.
+        if not self.running_in_thread: self.initiate_send()
     def push_with_producer (self, producer):
         self.producer_fifo.push (producer)
-------END PATCH----------


More information about the Python-Dev mailing list