By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,492 Members | 3,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,492 IT Pros & Developers. It's quick & easy.

Asyncore select statement problem

P: n/a
I have a problem with python's asyncore module throwing a bad file
descriptor error. The code might be difficult to copy here, but the
problem is essentially:

The server wants to sever the connection of an open Asyncore socket.
Calling the socket.close() nor the socket.shutdown(2) calls seem to
work. The only way I can close the connection without creating the
error below is to have the client close the connection.

I have the asyncore.loop() as the last line of a thread that is spawned
within the applications "mainframe.py" or gui thread. It doesn't seem
to me like this would make a difference, but I am unfamiliar with the
specifics of how the asyncore module works.

Any thoughts people have would be greatly appreciated. If needed I may
be able to create a small version of the problem to post for people to
see.

Thanks,
Jim Howard
Exception in thread Thread-1:
Traceback (most recent call last):
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",
line 460, in __bootstrap
self.run()
File
"/Users/jwhoward2/Documents/Projects/LJServer/LJDeviceServer/DeviceServer.py",
line 23, in run
asyncore.loop()
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/asyncore.py",
line 191, in loop
poll_fun(timeout, map)
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/asyncore.py",
line 121, in poll
r, w, e = select.select(r, w, e, timeout)
error: (9, 'Bad file descriptor')

Jan 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try using another ascyncore example and see if that works for you.
Maybe, first one without threading, like
<http://effbot.org/lib/asyncore.html#asyncore-example-basic-http-client>

Asyncore worked fine on my application on Linux, but haven't tried that
on MacOS.

/Jean Brouwers

JamesHoward wrote:
I have a problem with python's asyncore module throwing a bad file
descriptor error. The code might be difficult to copy here, but the
problem is essentially:

The server wants to sever the connection of an open Asyncore socket.
Calling the socket.close() nor the socket.shutdown(2) calls seem to
work. The only way I can close the connection without creating the
error below is to have the client close the connection.

I have the asyncore.loop() as the last line of a thread that is spawned
within the applications "mainframe.py" or gui thread. It doesn't seem
to me like this would make a difference, but I am unfamiliar with the
specifics of how the asyncore module works.

Any thoughts people have would be greatly appreciated. If needed I may
be able to create a small version of the problem to post for people to
see.

Thanks,
Jim Howard
Exception in thread Thread-1:
Traceback (most recent call last):
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",
line 460, in __bootstrap
self.run()
File
"/Users/jwhoward2/Documents/Projects/LJServer/LJDeviceServer/DeviceServer.py",
line 23, in run
asyncore.loop()
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/asyncore.py",
line 191, in loop
poll_fun(timeout, map)
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/asyncore.py",
line 121, in poll
r, w, e = select.select(r, w, e, timeout)
error: (9, 'Bad file descriptor')
Jan 18 '07 #2

P: n/a
"JamesHoward" <Ja************@gmail.comescribió en el mensaje
news:11**********************@q2g2000cwa.googlegro ups.com...
>I have a problem with python's asyncore module throwing a bad file
descriptor error. The code might be difficult to copy here, but the
problem is essentially:

The server wants to sever the connection of an open Asyncore socket.
Calling the socket.close() nor the socket.shutdown(2) calls seem to
work. The only way I can close the connection without creating the
error below is to have the client close the connection.
You have to use the dispatcher's close() method, else, the asyncore map
won't be updated, keeping a reference to the closed socket.

--
Gabriel Genellina
Jan 18 '07 #3

P: n/a
Thank you for the responses. I have learned considerably more about
how Asyncore works because of it.

The problem that I see is that Asyncore's poll function does not seem
to be thread safe. From what I can tell, I am calling
dispatcher.close() properly and the dispatchers are removed from
asyncore's global map (all except the server itself). However, it
seems like the error happens when the poll function gets the file
descriptors to run select on and then the thread ticks and removes them
from the global map. After this the select call is made, but the file
descriptors are not valid anymore.

I guess I have two questions as a result. First, is this a problem
that anyone else has had and second is there a fix for it? I have
tried looking for Asyncore thread safe topics in Google, but without
much luck. If needed I think making the poll function atomic in the
asyncore module might fix this problem, but I wanted to see what other
people thought first.

Thanks again for the help,
Jim Howard
Gabriel Genellina wrote:
"JamesHoward" <Ja************@gmail.comescribió en el mensaje
news:11**********************@q2g2000cwa.googlegro ups.com...
I have a problem with python's asyncore module throwing a bad file
descriptor error. The code might be difficult to copy here, but the
problem is essentially:

The server wants to sever the connection of an open Asyncore socket.
Calling the socket.close() nor the socket.shutdown(2) calls seem to
work. The only way I can close the connection without creating the
error below is to have the client close the connection.

You have to use the dispatcher's close() method, else, the asyncore map
won't be updated, keeping a reference to the closed socket.

--
Gabriel Genellina
Jan 19 '07 #4

P: n/a
At Friday 19/1/2007 14:42, JamesHoward wrote:
>Thank you for the responses. I have learned considerably more about
how Asyncore works because of it.

The problem that I see is that Asyncore's poll function does not seem
to be thread safe. From what I can tell, I am calling
dispatcher.close() properly and the dispatchers are removed from
asyncore's global map (all except the server itself). However, it
seems like the error happens when the poll function gets the file
descriptors to run select on and then the thread ticks and removes them
from the global map. After this the select call is made, but the file
descriptors are not valid anymore.

I guess I have two questions as a result. First, is this a problem
that anyone else has had and second is there a fix for it? I have
tried looking for Asyncore thread safe topics in Google, but without
much luck. If needed I think making the poll function atomic in the
asyncore module might fix this problem, but I wanted to see what other
people thought first.
Usually asyncore is used with only one thread - that's one of the
reasons of using it btw. You can spawn other working threads, but
they don't usually interact with the networking stuff directly.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 19 '07 #5

P: n/a
Again, thank you for your help. With digging through the Asyncore.py
source, I was able to find the poll2 function which is called when the
function asyncore.loop(use_poll = True) is enabled.

This function does not use a select call, but a poll call to do its
looping. It works well for the problem of threads closing devices at
unknown times. The reason for this is that the select statement is
called on a series of file descriptors that should not be changed. If
the file descriptors become invalid, select throws and exception and
the asyncore loop haults. The use_poll flag sets the asyncore module
to use a poll instead of a select statement.

Within the poll method, there are better ways of dealing with file
descriptors and they seem to be able to discern if a file descriptor
becomes disconnected with the POLLHUP flag.

I am still unsure as to why the select function is used instead of the
poll function, but using poll appears to have solved my problem.

Thanks for all the help,
Jim Howard

Jan 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.