Gon?alo Rodrigues wrote:

My setup is the following: I have socket s from which I want to read
and write. So I made the following set up:

There is a thread whose only job is to read. Any data read (from recv
call) is just passed to (some) Queue. This thread is "owned" by a
second thread waiting on a Queue for write requests. The thread just
pops these from the Queue, and calls the send method from the socket.
This thread also takes care of closing the socket or (possibly)
handling any exceptions raised due to socket operation.

So my question is: since I have two threads sharing the same socket,
even though one is only reading and the other does everything else, do
I have to watch out for any "concurrency" issues?

P.S: This is for learning experience. So it's of no use telling me
that I should learn Twisted :-) I may (eventually) get there, but at
the moment I feel more omfortable with working with plain blocking
The first problem I can think of is the one that stopped me. Note the
code below... You can't close a blocked socket in python even from a
separate thread.

import unittest
import socket
import threading
import time

class SocketAcceptor ( threading.Thread ):
def __init__( self, socket ):
threading.Thread.__init__( self )
self.socket = socket
self.done = 0

def run( self ):
self.socket.bind( ( "", 3424 ) )
self.socket.listen( 5 )
child, ip = self.socket.accept()
self.done = 1

class SocketTester ( unittest.TestCase ):
def testClose( self ):
ss = socket.socket()
acceptor_thread = SocketAcceptor( ss )
time.sleep( 1 )
time.sleep( 1 )
self.assertEquals( acceptor_thread.done, 1 )

if __name__ == '__main__':

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
grouppython-list @
postedSep 1, '03 at 1:22a
activeSep 2, '03 at 1:59p

2 users in discussion

Gonçalo Rodrigues: 2 posts Daniel T.: 1 post



site design / logo © 2022 Grokbase