473,785 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing socket file descriptors

Hi, I'm experiencing a problem when trying to close the file descriptor
for a socket, creating another socket, and then closing the file
descriptor for that second socket. I can't tell if my issue is about
Python or POSIX.

In the following, the first time through, everything works. On the
second connection, though, the same file descriptor as the first
connection may be re-used, but for some reason, trying to do
os.read/close on that file descriptor will cause an error.

Thanks in advance for any help on why this problem is occurring and/or
how to resolve it (preferrably, I can continue to use file descriptors
instead of resorting to socket.recv/socket.close).

def handle( s ):
print id(s), s
print os.read( s.fileno(), 4096 ) # s.recv(4096)
os.close( s.fileno() ) # s.close()
svr = socket.socket()
svr.bind( ( 'localhost', 8003 ) )
svr.listen( 1 )
while True:
print 'accepting'
s,_ = svr.accept()
handle( s )

# Traceback (most recent call last):
# File "./normal_server_c lose_error.py", line 25, in <module>
# handle( s )
# File "./normal_server_c lose_error.py", line 13, in handle
# print os.read( s.fileno(), 4096 ) # s.recv(4096)
# OSError: [Errno 9] Bad file descriptor
May 20 '07 #1
3 5273
js
Hello, Yang.

You're not supposed to use os.open there.
See the doc at http://docs.python.org/lib/os-fd-ops.html

Is there any reason you want to use os.close?

On 20 May 2007 04:26:12 GMT, Yang
<DE************ *****@mailnulld eletethis.comwr ote:
Hi, I'm experiencing a problem when trying to close the file descriptor
for a socket, creating another socket, and then closing the file
descriptor for that second socket. I can't tell if my issue is about
Python or POSIX.

In the following, the first time through, everything works. On the
second connection, though, the same file descriptor as the first
connection may be re-used, but for some reason, trying to do
os.read/close on that file descriptor will cause an error.

Thanks in advance for any help on why this problem is occurring and/or
how to resolve it (preferrably, I can continue to use file descriptors
instead of resorting to socket.recv/socket.close).

def handle( s ):
print id(s), s
print os.read( s.fileno(), 4096 ) # s.recv(4096)
os.close( s.fileno() ) # s.close()
svr = socket.socket()
svr.bind( ( 'localhost', 8003 ) )
svr.listen( 1 )
while True:
print 'accepting'
s,_ = svr.accept()
handle( s )

# Traceback (most recent call last):
# File "./normal_server_c lose_error.py", line 25, in <module>
# handle( s )
# File "./normal_server_c lose_error.py", line 13, in handle
# print os.read( s.fileno(), 4096 ) # s.recv(4096)
# OSError: [Errno 9] Bad file descriptor
--
http://mail.python.org/mailman/listinfo/python-list
May 20 '07 #2
Hi, thanks for your answer. Should I just use that object's close()
method? Is it safe to assume that objects that have fileno() also have
close()? (Statically typed interfaces would come in handy now.)

I'm writing a simple asynchronous I/O framework (for learning purposes -
I'm aware of the myriad such frameworks for Python), and I'm writing a
wrapper around objects that can be passed into select.select() . Since
select() requires objects that have fileno's, that's the only
requirement I place on the wrapped object's interface, and thus why I've
been using FD-based operations:

class handle( object ):
'''handle( underlying_file ) -handle object

A wrapper for a nonblocking file descriptor/handle. Wraps any object
with a fileno() method.'''

__slots__ = [ '_real' ]
# _real is the underlying file handle. Must support fileno().

def __init__( self, real_handle ):
self._real = real_handle

def fileno( self ):
return self._real.file no()

def close( self ):
close( self._real.file no() )
# seems that this must now become: self._real.clos e()

def wait_readable( self ):
...

"js " <eb*****@gmail. comwrote in
news:ma******** *************** *************** *@python.org:
Hello, Yang.

You're not supposed to use os.open there.
See the doc at http://docs.python.org/lib/os-fd-ops.html

Is there any reason you want to use os.close?

On 20 May 2007 04:26:12 GMT, Yang
<DE************ *****@mailnulld eletethis.comwr ote:
>Hi, I'm experiencing a problem when trying to close the file
descriptor
>for a socket, creating another socket, and then closing the file
descriptor for that second socket. I can't tell if my issue is about
Python or POSIX.

In the following, the first time through, everything works. On the
second connection, though, the same file descriptor as the first
connection may be re-used, but for some reason, trying to do
os.read/close on that file descriptor will cause an error.

Thanks in advance for any help on why this problem is occurring
and/or
>how to resolve it (preferrably, I can continue to use file
descriptors
>instead of resorting to socket.recv/socket.close).

def handle( s ):
print id(s), s
print os.read( s.fileno(), 4096 ) # s.recv(4096)
os.close( s.fileno() ) # s.close()
svr = socket.socket()
svr.bind( ( 'localhost', 8003 ) )
svr.listen( 1 )
while True:
print 'accepting'
s,_ = svr.accept()
handle( s )

# Traceback (most recent call last):
# File "./normal_server_c lose_error.py", line 25, in <module>
# handle( s )
# File "./normal_server_c lose_error.py", line 13, in handle
# print os.read( s.fileno(), 4096 ) # s.recv(4096)
# OSError: [Errno 9] Bad file descriptor
--
http://mail.python.org/mailman/listinfo/python-list
May 20 '07 #3
js
Hi Yang.
Hi, thanks for your answer. Should I just use that object's close()
method? Is it safe to assume that objects that have fileno() also have
close()? (Statically typed interfaces would come in handy now.)
I'm writing a simple asynchronous I/O framework (for learning purposes -
I'm aware of the myriad such frameworks for Python), and I'm writing a
wrapper around objects that can be passed into select.select() . Since
select() requires objects that have fileno's, that's the only
requirement I place on the wrapped object's interface, and thus why I've
been using FD-based operations:
I'm not sure whether objects which have fileno always have close,
but I think it's always safe to use the object's close method.
How about keeping the wrapped object's interface consistent
in your framework?
It'd make your work moch easier.
May 20 '07 #4

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

Similar topics

1
3796
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
8
3638
by: J Peterman | last post by:
I need to do this exercise, but am having problems. I need to write a program that firstly, sleeps for 5 seconds, then reads a line of input from file descriptor 0 and then writes the line back to file descriptor 1. Apparrantly, this program would block forever unless you type a line of text on the keyboard, but I can't get a working program to try. Next, this program needs to be modified so that it would write its input
5
3556
by: JG | last post by:
Hi all, Does anyone know how the implementations on Linux and Windows handle synchronization between a read and write FD open to the same file. For example, if I have 2 FD open to file X.txt. 1 I use for reading, the other for writing. If I write to position 125 on the write_FD, call flush, and then turn around and read from the read_FD position 125, am I guaranteed to get the result I just wrote?
1
2067
by: upendrao | last post by:
Hi, I have a requirement in which I need to count the number of file descriptors associated with a file. To be more clear...A file can be opened by several processes say in read only mode. I would like to count the number of such file opens on particular file at a given instant of time. Any C library function or unix command that can solve my problem please...?
4
5830
by: Joe Lester | last post by:
I'm trying to figure out what the optimal Postgres configuration would be for my server (with 200 connecting clients, even though I'd really like to get it up to 500). I've got a 700 MHz eMac running Mac OS 10.3.2 (Panther) with 512 MB of RAM. I've messed around with some settings but I'm still getting an occasional "out of file descriptor" error, especially when performing a VACUUM. Like so... 2004-04-13 23:30:05 LOG: out of file...
0
1384
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
2
5442
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads are reading the content written into the file. Can anybody tell me is there a performance or any other gain (except for the multex locking) by using different file descriptors in each thread for the same file rather than using a single FD with a...
15
3188
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the full analysis start. My problem is that the FILE* I want do parse has been fopen'ed far away from where I am and I don't know in which MODE my FILE* has been opened. And additionally, my FILE* may not be a regular file, but a continuous
3
3497
by: Roger Davis | last post by:
Hi, I am brand-new to Python but am an experienced C/Unix programmer. I am rewriting in Python some old shell scripts that do lots of stuff like set output = `cat this | grep that | whatever ...` My sense is that this should be done using subprocess.Popen() since, although I am using Python 2.4 (Linux) and 2.6 (MacOS), all of the other library interfaces for this sort of thing are being deprecated/removed in future releases. I have...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7502
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
6741
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
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.