473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help needed in problem with Threads and sockets.

Hi,

I have a problem with threads and sockets. I'll try to describe the
problem in words with pseudo-code.

I've been working on a few classes to make it easier to work with
threads. This framework, let us call it that, consists in two parts.
The first part is just a basic thread class deriving from
threading.Thread with a few extra functionality that makes it easier
for a thread to spawn a new thread and "father it". Each of its
instances also has a queue.Queue where other threads can post data.
The other part is an Event class, whose instances are traded among
threads (via their queues). These Event classes also know how to
process themselves by calling a sequence of handlers and things are
made up in such a way that it's easy to add or remove handlers at
runtime.

In testing this framework, and also with a mind for future
applications, I tried to add sockets to the mix.

The basic idea was that a socket was shared among two threads. The
*only* job of one of the threads is to listen to the socket and read
whatever is there, while the other socket listens to incoming requests
(in the form of Event's) like: open the socket, write to the socket,
close the socket, kill yourself, etc.

For a number of reasons, instead of dealing with sockets directly I
found it easier to deal with streams. So I just wrapped the socket in
a stream-like class. The stream class is little more than a thin
wrapper around socket.makefile(). In fact the whole thing is just:

class SocketProxy(object):

def __init__(self, sockfile):
super(SocketProxy, self).__init__(self)
self._sockfile = sockfile

def __getattr__(self, attrib):
return getattr(self._sockfile, attrib)

def write(self, s):
self._sockfile.write(s)
#Force a flush of the underlying socket.
self._sockfile.flush()

And it's instantiated as SocketProxy(<some socket>.makefile()).

The structure run method of the read thread TReadStream is basically
(in pseudo-code and leaving out some gobbledigook dealing with
exception handling)

while self.exit:
<select call with timeout>
if <stream is readable>:
data = <stream>.read()
if data:
<push data to parent thread>

The select calls with timeout are needed so that we can check the exit
flag and have the opportunity to kill the thread.

The run method of the write thread TWriteStream is basically:

<process a StartStreamEvent event>
try:
while True:
<pop an event from the queue>
try:
<process event>
except Exception, e:
<process error event>
finally:
<process an EndStreamEvent event>

Then there is a TStream(TWriteStream) whose only difference from
TWriteStream is in __init__ that instantiates a read thread
TReadStream and "fathers it" so that the read thread knows where to
push the data.

Now, all the tests for TReadStream and TWriteStream are passing which
means that their basic functionality is working OK. But in testing
TStream things fails miserably.

The test that fails is basically this:

<Write to the stream>
<Test that you read back is exactly what you sent.>

where the stream wraps a socket connected to an echo server => just
echoes back whatever it receives.

I added a few print statements here and there and the output I get is:

StartStreamEvent(None, priority = 1)
Select call.
WriteEvent('A test.', priority = 0)
Reading data.

This means that the following happened:

1. The thread started ok (StartStreamEvent processed). This means that
TStream was started and it's child threads (a TReadStream in this
case) started.
2. The read thread is about to make a select call.
3. A WriteEvent was processed (=> "A test." was sent to the stream).
4. The select call finished, the stream was readable and we're about
to read from the stream.

Then things hang up. The read call never finished, the data was not
pushed to the TStream, etc.

Anybody knows what might be the problem?

BTW, this is in Win2K. If you need more accurate descriptions (exact
code, etc.) just holler.

TIA, With my best regards,
G. Rodrigues
Jul 18 '05 #1
0 1425

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
4
by: Julia Goolia | last post by:
hello, i read that it is bad to use threads with tkinter. so my question is how does one create a gui program with sockets? at one point you have to call mainloop() which does not return. ...
6
by: Benjamin Walling | last post by:
We have 109 remote offices all running Sybase ASA Server. We collect data from these offices and consolidate it into our main server. I have written a program that will read from each office and...
4
by: nyhetsgrupper | last post by:
Hi, I've written a async server app. This app start by connecting to a client and then send some data (BeginSend). When the data is sent, the server is starting to listen for incomming data....
4
by: nyhetsgrupper | last post by:
I'm writing a server application connection to multiple clients using sockets. I want one socket for each client, and the comunication needs to be async. I could use the async sockets methods...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
1
by: Karim | last post by:
Hi, I am working on a c++ project involving sockets and threads and I need to ask a pretty basic question. If you have a socket that every time it accepts a connection (accept()) it spawns a...
8
by: Chizl | last post by:
I'm building a web server and having some issues with the TCPListener.Start(BackLog). It doesn't seem to do as expected. I'm using MS Web Stress Tool to test against my web server and when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.