Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > threads and socket question

Reply
Thread Tools

threads and socket question

 
 
Gonšalo Rodrigues
Guest
Posts: n/a
 
      09-01-2003
Hi,

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
sockets.

With my best regards,
G. Rodrigues
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      09-01-2003
Gon?alo Rodrigues <(E-Mail Removed)> 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
> sockets.


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 )
try:
child, ip = self.socket.accept()
except:
pass
self.done = 1

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

if __name__ == '__main__':
unittest.main()
 
Reply With Quote
 
 
 
 
Gonšalo Rodrigues
Guest
Posts: n/a
 
      09-02-2003
On Mon, 01 Sep 2003 01:34:32 GMT, "Daniel T."
<(E-Mail Removed)> wrote:

>Gon?alo Rodrigues <(E-Mail Removed)> 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
>> sockets.

>
>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.
>


Thanks for this piece of info. My experiences with my setup confirm
it.

I really want to keep the socket-closing in the write thread so my
problem is reduced to be able to order to read thread to die. What I
chose to do is to have it periodically check some exit flag to know
when to exit. I believe this can be done by calling select with a
timeout before the recv blocking call. Back to the docs and
Python-experimenting-mode.

With my best regards,
G. Rodrigues
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments