Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > select.epoll question

Reply
Thread Tools

select.epoll question

 
 
Paul Rubin
Guest
Posts: n/a
 
      02-07-2013
I'm trying to listen to a bunch of sockets using epoll under Linux, e.g.

import select, socket
socket1 = socket.socket() ...

p = select.epoll()
p.register(socket1); p.register(socket2); ...
result = p.poll()

This returns `result' as a list of 2-tuples (fd, status) where fd
is a Linux file descriptor, i.e. a small integer like comes back
from socket.fileno(). That's different from select.select, which
returns a list of actual socket objects that I can read from with
socket.recv().

Any idea of a good way to map the file descriptors back to socket
objects? Is there some kind of hidden interface that I don't know
about, that gives back sockets directly?

The docs for epoll in select.html don't seem very good. I haven't
yet examined the source code.

Thanks for any advice.

--Paul
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      02-07-2013
On Thu, Feb 7, 2013 at 6:08 PM, Paul Rubin <(E-Mail Removed)> wrote:
> Any idea of a good way to map the file descriptors back to socket
> objects? Is there some kind of hidden interface that I don't know
> about, that gives back sockets directly?


I don't know of any, but you can get the file descriptor from a socket
via fileno(), and build your own dictionary:

fd_to_sock={sock.fileno():sock for sock in list_of_sockets}

You'd need to manually maintain that as sockets get created/destroyed, though.

ChrisA
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      02-07-2013
Chris Angelico <(E-Mail Removed)> writes:
> fd_to_sock={sock.fileno():sock for sock in list_of_sockets}
> You'd need to manually maintain that as sockets get created/destroyed,
> though


Thanks, I was hoping to avoid that. I'll have to check how
select.select manages to return sockets. Maybe it builds such a dict
from the object list before it calls the system's select function, then
maps the result back afterwards. Ugh.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-07-2013
On Fri, Feb 8, 2013 at 3:15 AM, Paul Rubin <(E-Mail Removed)> wrote:
> Chris Angelico <(E-Mail Removed)> writes:
>> fd_to_sock={sock.fileno():sock for sock in list_of_sockets}
>> You'd need to manually maintain that as sockets get created/destroyed,
>> though

>
> Thanks, I was hoping to avoid that. I'll have to check how
> select.select manages to return sockets. Maybe it builds such a dict
> from the object list before it calls the system's select function, then
> maps the result back afterwards. Ugh.


Yeah, I figured fileno() probably wouldn't be news to you. I don't
suppose there's anything convenient in the rest of your application
that makes such a list/dict plausible? For instance, if you need to
maintain a list of all current socket connections to support broadcast
operations, then it's not much harder to also maintain the fd->socket
mapping.

ChrisA
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-07-2013
Chris Angelico <(E-Mail Removed)> writes:
> Yeah, I figured fileno() probably wouldn't be news to you. I don't
> suppose there's anything convenient in the rest of your application
> that makes such a list/dict plausible?


In fact it's rather annoying, sockets are created and destroyed in
multiple places in the program. I have to be careful about leaking them
in case a thread crashes, etc. I dealt with the poll issue by manually
tracking what was happening, but it wasn't pretty, so I wondered if
there was a better solution I was overlooking. I don't want to mess
with it too much more since I'm planning a completely different approach
for the next version of the program.
 
Reply With Quote
 
Kushal Kumaran
Guest
Posts: n/a
 
      02-08-2013
Paul Rubin <(E-Mail Removed)> writes:

> Chris Angelico <(E-Mail Removed)> writes:
>> Yeah, I figured fileno() probably wouldn't be news to you. I don't
>> suppose there's anything convenient in the rest of your application
>> that makes such a list/dict plausible?

>
> In fact it's rather annoying, sockets are created and destroyed in
> multiple places in the program. I have to be careful about leaking them
> in case a thread crashes, etc. I dealt with the poll issue by manually
> tracking what was happening, but it wasn't pretty, so I wondered if
> there was a better solution I was overlooking. I don't want to mess
> with it too much more since I'm planning a completely different approach
> for the next version of the program.


You don't have to maintain the mapping globally. You could wrap
socket.epoll in a class of your own that manages the mapping, and passes
through calls to socket.epoll. That would make the wrapper a drop-in
replacement for socket.epoll.

--
regards,
kushal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments