473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threa d 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(obj ect):

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

def __getattr__(sel f, attrib):
return getattr(self._s ockfile, 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(<so me socket>.makefil e()).

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 StartStreamEven t 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(TWriteS tream) 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:

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

This means that the following happened:

1. The thread started ok (StartStreamEve nt 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 1446

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

Similar topics

2
3891
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 support for sockets to the system. Thread needs to unblock when: - there is socket ready to be read, or
4
7543
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. then you are not free to do stuff with the sockets. what's up with that? thanks so much for your time!
6
1345
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 synchronize the data. It takes a while to run because each office only has a 128k Frame Relay. I have two database classes: one for accessing the office, one for accessing the main server. The database classes expose simple functions of...
4
2138
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. (BeginRecieve). In the receive callback I always call BeginRecieve again to keep listen for more data. The server is continualy listening, and from time to time sending (but if it is sending more data when there is already a listening socket, I don't...
4
4577
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 (beginsend, beginrecv...) or I could start a new thread for each client and run the comunication using the sync mehtods (send,recv). What is the best aproach? This is a performance critical application.
15
2575
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 determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
2
4549
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 nothing to control Timeout Connection... and I wasn't interested to wait 25-30sec for each ip. After some search, it's look like Socket was the solution with
1
1529
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 thread with the identifier returned by accept where the thread will write to the identifier using send(). My question is, are those identifiers returned by accept unique and
8
8932
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 see 200 connections at once to it, and I'm seeing 2/3 of the sockets as socket errors.. They never get into my code, so it has to be failing on the connection side.. Backlog is the only thing I can think could be the problem. ...
0
8707
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7953
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.