472,351 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Asyncore select statement problem

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
5 6953
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Garth | last post by:
Hi, I think there's an error in ther socketmodule.c code under windows. The code in internal connect should test exceptfds to check for...
5
by: David M. Wilson | last post by:
Hi peeps, I finally got around recently to doing something serious using Python for serving network clients asynchronously, deciding on asyncore...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore...
0
by: Tony Meyer | last post by:
Changes in asyncore from 2.3 to 2.4 mean that asyncore.poll() now passes all the sockets in the map to select.select() to be checked for errors,...
1
by: Jos | last post by:
Hello all. I have a working server, using the asyncore/chat module, which enables logging in, rooms and private messaging. I've used this...
0
by: Z. Kotzer | last post by:
I can not get error notifications when an asynchat based client tries to connect to a non-responsive address. To validate the problem I changed...
0
by: Daniel Walton | last post by:
I have been working on a problem for a full week now. If someone could please help me it would be great. Even a clue would be great at this...
7
by: billie | last post by:
Hi all. I've just terminated a server application using asyncore / asynchat frameworks. I wrote a test script that performs a lot of connections to...
8
by: Frank Millman | last post by:
Hi all I have been using my own home-brewed client/server technique for a while, using socket and select. It seems to work ok. The server can...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.