473,698 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select hangs after some reads

Hi,

I'm building a multithreaded application and I encountered a tiny and
annoying problem. I use a select to wait for data to be read from a
socket, after some reads, the select simply blocks and stays that way
until I close the connection on the other side of the socket. When the
socket is closed on the writer end the select releases and then I get
only empty strings from the socket.
My question is this: Why did it block? The reading has never ended,
every test I make I write 50 requests (wich are strings) to the socket
and I have read the maximum of 34 requests. I'm using winPdb to take a
closer look on what's happening and I see the threads blocked on this
same select. If I send anything more through the socket, the select
releases for a thread, despite the other data that is still unread.

This is the select:
rd,w,e = select.select([self.rfd],[],[])

self.rfd is the fileno of the file object returned by the makefile
method from the socket object.

I know that's some buffer behavior that I'm missing but I don't know
what it is.

anything is helpfull, If I'm beeing stupid you can say it.
thanks

Jun 8 '06 #1
14 1941
al**********@gm ail.com wrote:
Hi,

I'm building a multithreaded application and I encountered a tiny and
annoying problem. I use a select to wait for data to be read from a
socket, after some reads, the select simply blocks and stays that way
until I close the connection on the other side of the socket. When the
socket is closed on the writer end the select releases and then I get
only empty strings from the socket.
That's to be expected: the first return of a zero-length string from
socket.read() indicates end of file.
My question is this: Why did it block? The reading has never ended,
every test I make I write 50 requests (wich are strings) to the socket
and I have read the maximum of 34 requests. I'm using winPdb to take a
closer look on what's happening and I see the threads blocked on this
same select. If I send anything more through the socket, the select
releases for a thread, despite the other data that is still unread.
Are you sure that the received data is presenting in block the same size
as are being sent? There's no guarantee this will be so on a TCP socket,
and it may be that multiple sends are being coalesced into a single
read. The important thing to focus on is the total number of bytes sent
and received.
This is the select:
rd,w,e = select.select([self.rfd],[],[])

self.rfd is the fileno of the file object returned by the makefile
method from the socket object.
It would be simpler to use the result of the socket's .fileno() method
directly.
I know that's some buffer behavior that I'm missing but I don't know
what it is.

anything is helpfull, If I'm beeing stupid you can say it.
thanks

Without being able to see all your code it's hard to say whether or not
you are doing something daft, but you give a general impression of
competence.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 8 '06 #2

Steve Holden wrote:
al**********@gm ail.com wrote:
Hi,

I'm building a multithreaded application and I encountered a tiny and
annoying problem. I use a select to wait for data to be read from a
socket, after some reads, the select simply blocks and stays that way
until I close the connection on the other side of the socket. When the
socket is closed on the writer end the select releases and then I get
only empty strings from the socket.
That's to be expected: the first return of a zero-length string from
socket.read() indicates end of file.
My question is this: Why did it block? The reading has never ended,
every test I make I write 50 requests (wich are strings) to the socket
and I have read the maximum of 34 requests. I'm using winPdb to take a
closer look on what's happening and I see the threads blocked on this
same select. If I send anything more through the socket, the select
releases for a thread, despite the other data that is still unread.

Are you sure that the received data is presenting in block the same size
as are being sent? There's no guarantee this will be so on a TCP socket,
and it may be that multiple sends are being coalesced into a single
read. The important thing to focus on is the total number of bytes sent
and received.


Well, actually I´m using a very simple protocol wich sends only
strings ended by newline. I need to send 3 chunks of information and a
newline after them. On the reader side I make 3 readline(), this way I
wouldn´t have to care about this problem, but maybe that´s where I´m
falling. If that´s the case, I´ll have to use a more complex
protocol.
This is the select:
rd,w,e = select.select([self.rfd],[],[])

self.rfd is the fileno of the file object returned by the makefile
method from the socket object.
It would be simpler to use the result of the socket's .fileno() method
directly.

Yes, you are right, this is something remanescent from old ideas.
I know that's some buffer behavior that I'm missing but I don't know
what it is.

anything is helpfull, If I'm beeing stupid you can say it.
thanks

Without being able to see all your code it's hard to say whether or not
you are doing something daft, but you give a general impression of
competence.


Thanks, the code has a significant size now, if I don´t solve it
quickly I´ll post the most important parts.

thanks again,

Andre LS Meirelles regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


Jun 8 '06 #3
On 2006-06-08, al**********@gm ail.com <al**********@g mail.com> wrote:
Well, actually I´m using a very simple protocol wich sends only
strings ended by newline. I need to send 3 chunks of information and a
newline after them. On the reader side I make 3 readline(), this way I
wouldn´t have to care about this problem, but maybe that´s where I´m
falling. If that´s the case, I´ll have to use a more complex
protocol.


You can't use readline() with select(). Select tells you
whether recv() called on the underlying socket will block or
not. What's probably happening is that all of the data has
been read from the underlying socket and is being held in a
buffer waiting to be read by readline().

The Select call has no way of knowing about that buffered data.
As far as it's concerned there's no more data left to read, so
it block until the socket is closed.

--
Grant Edwards grante Yow! My mind is a potato
at field...
visi.com
Jun 8 '06 #4

Grant Edwards escreveu:
On 2006-06-08, al**********@gm ail.com <al**********@g mail.com> wrote:
Well, actually I´m using a very simple protocol wich sends only
strings ended by newline. I need to send 3 chunks of information and a
newline after them. On the reader side I make 3 readline(), this way I
wouldn´t have to care about this problem, but maybe that´s where I´m
falling. If that´s the case, I´ll have to use a more complex
protocol.
You can't use readline() with select(). Select tells you
whether recv() called on the underlying socket will block or
not. What's probably happening is that all of the data has
been read from the underlying socket and is being held in a
buffer waiting to be read by readline().


Yes, as I expected, its the buffers. In my opinion the problem is that
the socket module
doesn't provide a way of reading all its internal buffer.

readlines() just make subsequent calls to readline and readline may
call recv, so we have a locked scene. I want to know if I will block
anyway. Of course I can clean the buffer myself, but I think the socket
module should provide a way of doing this. It's not a big problem
though.

The Select call has no way of knowing about that buffered data.
As far as it's concerned there's no more data left to read, so
it block until the socket is closed.
You're very right.
thanks,

Andre LS Meirelles
--
Grant Edwards grante Yow! My mind is a potato
at field...
visi.com


Jun 8 '06 #5
al**********@gm ail.com wrote:
Grant Edwards escreveu:

On 2006-06-08, al**********@gm ail.com <al**********@g mail.com> wrote:

Well, actually I´m using a very simple protocol wich sends only
strings ended by newline. I need to send 3 chunks of information and a
newline after them. On the reader side I make 3 readline(), this way I
wouldn´t have to care about this problem, but maybe that´s where I´m
falling. If that´s the case, I´ll have to use a more complex
protocol.


You can't use readline() with select(). Select tells you
whether recv() called on the underlying socket will block or
not. What's probably happening is that all of the data has
been read from the underlying socket and is being held in a
buffer waiting to be read by readline().

Yes, as I expected, its the buffers. In my opinion the problem is that
the socket module
doesn't provide a way of reading all its internal buffer.

readlines() just make subsequent calls to readline and readline may
call recv, so we have a locked scene. I want to know if I will block
anyway. Of course I can clean the buffer myself, but I think the socket
module should provide a way of doing this. It's not a big problem
though.
The Select call has no way of knowing about that buffered data.
As far as it's concerned there's no more data left to read, so
it block until the socket is closed.

You're very right.
thanks,

Of course, if the client forces the TCP PSH flag true then the receiver
is guaranteed to debuffer the stream up to that point - this is how FTP
clients work, for example.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 8 '06 #6
al**********@gm ail.com wrote:
Yes, as I expected, its the buffers. In my opinion the problem is that
the socket module doesn't provide a way of reading all its internal buffer.


umm. that's what recv() does, of course, of you pass in a large enough
buffersize.

for your use case, I suggest looking at asyncore/asynchat instead of
trying to write your own asynchronous socket layer...

</F>

Jun 8 '06 #7
On 2006-06-08, al**********@gm ail.com <al**********@g mail.com> wrote:
Well, actually I´m using a very simple protocol wich sends
only strings ended by newline. I need to send 3 chunks of
information and a newline after them. On the reader side I
make 3 readline(), this way I wouldn´t have to care about this
problem, but maybe that´s where I´m falling. If that´s the
case, I´ll have to use a more complex protocol.
You can't use readline() with select(). Select tells you
whether recv() called on the underlying socket will block or
not. What's probably happening is that all of the data has
been read from the underlying socket and is being held in a
buffer waiting to be read by readline().


Yes, as I expected, its the buffers. In my opinion the problem
is that the socket module doesn't provide a way of reading all
its internal buffer.


What internal buffer?
readlines() just make subsequent calls to readline and
readline may call recv, so we have a locked scene. I want to
know if I will block anyway.
I'm lost.
Of course I can clean the buffer myself, but I think the
socket module should provide a way of doing this.


I'm afraid I don't understand what you mean.

Since you talked about calling readline(), I assumed that you
had called the socket object's makefile() method to create a
file-object. The select system call can only tell you whether
a recv on the socket will block or not. It knows nothing about
the state of the file object on which you're calling readline().

--
Grant Edwards grante Yow! Is this an out-take
at from the "BRADY BUNCH"?
visi.com
Jun 8 '06 #8
In article <ma************ *************** ************@py thon.org>, Steve Holden wrote:
Of course, if the client forces the TCP PSH flag true then the receiver
is guaranteed to debuffer the stream up to that point - this is how FTP
clients work, for example.


I don't think that's right. You are confusing the PSH flag (which is
basically unused in Unix networking I think) and the URG flag (which
is extremely rarely used, but is indeed used by FTP to get abort
requests to 'jump the queue' as it were).
Jun 8 '06 #9
Jon Ribbens wrote:
In article <ma************ *************** ************@py thon.org>, Steve Holden wrote:
Of course, if the client forces the TCP PSH flag true then the receiver
is guaranteed to debuffer the stream up to that point - this is how FTP
clients work, for example.

I don't think that's right. You are confusing the PSH flag (which is
basically unused in Unix networking I think) and the URG flag (which
is extremely rarely used, but is indeed used by FTP to get abort
requests to 'jump the queue' as it were).


Nope. The URG flag indicates that a packet contains out-of-band data,
whihc is what you describe above.

The PSH flag indicates that the data stream must be flushed right
through to the other end. This is essential for interactive protocols
such as FTP: without it the server has no way to know that the client
has sent a complete command, and vice versa.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 8 '06 #10

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

Similar topics

2
9362
by: Paulo Andre Ortega Ribeiro | last post by:
I have a Microsoft SQL Server 7.0. I wrote a sql command that creates a temporary table with a ORDER BY clause. When a execute a SELECT on this temporary table sometimes the result is ok, but sometimes is not ordered. I didn´t see anything like that. Any clue? Is there any kind of limits with temporary tables ? Because the command
3
4862
by: Senthuran | last post by:
Hi, The SQL server 2000 Server hangs some times. It is not periodic. It is not specific in any queries, which are taking more time to execute. Because, it is occurring for different types of applications on the same server on different machines. For the same applications when we had the SQL Server 7.0 we didn't have any problem. O/S: Windows 2000 advanced server Server: Dell power edge 2600 4 way server with 2 GB RAM
3
2491
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a command on another machine, but I need a timeout. I have to do this to a LOT of machines ( > 3000 ) and threading becomes necessary for timeliess. So I created a function which works with signals ( until you throw threading at it.. ) but I...
11
3022
by: Stefan Finzel | last post by:
Hi trying to remove one or all elements of select options fails for Pocket Internet Explorer. Is there a way to do this? if is_PIE { // this does not work on Pocket IE while (opt.length) { opt.remove(0); }
0
1614
by: Jeff | last post by:
Hi - I have an ASP.NET page that hangs (some times; if it loads for you, try refresh/F5 several times) when I try to run it on my localhost. It contains an HTML table for formatting, and within the table are several labels, textboxes, and validators. One of the textboxes is multiline and readonly, and the codebehind page has code to read a text file into it. (The text file is about 21,000 characters with spaces.) The aspx page has a...
2
11790
by: frank.sconzo | last post by:
Greetings, I was testing my web application on the Mac/Safari and noticed a problem with the background color of the select input element. Safari doesn't seem to pay any attention to the style I set for the background color. The listbox ends up with a white background. It works nicely in Internet Explorer and I would like to make it work on Safari too. Perhaps I am doing something slightly non-standard, or maybe there is a
2
3078
by: ThunderMusic | last post by:
Hi, I have a form in which there are many <asp:textbox> tags. When I run the form, we use tab to navigate across the textboxes, but one of our client noticed that when we navigate to some readonly textboxes, the cursor (text cursor, not mouse cursor) disapears and when we tab to the next textbox and come back (shift-tab), not it's ok. Notice that when we navigate to each textbox, the text is automaticaly selected which is not the case for...
5
8331
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE qry comes for a perticular row which is part of SELECT qry i want to know that whether UPDATE will wait for completing SELECT qry or not, or it simply executing without bothering SELECT qry .
9
3942
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result - I have read every post out there and spent hours trying to figure out the problem with no success whatsoever - I have constrained the problem to one form however, and I think it's hiding somewhere in my code associated with this form, which is...
1
8905
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
7743
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
6532
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
5869
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
4373
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
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.