473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 7069
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 connecttion refused. If this is so it should call...
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 as my starting point. After 2 days or so of...
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 module, you will realize that it's event loop does not...
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, which is probably a good thing. If an error occurs,...
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 framework to make some simple games that only required...
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 lib/test/test_asynchat.py as follows: class...
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 point. What I am seeing is quite a mystery. I have...
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 the server app and I discovered that asyncore...
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 handle multiple clients. It does this by creating a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.