POLL in different OSes

Lucio Torre lucio at movilogic.com
Tue Oct 23 19:20:23 EDT 2001


Lucio Torre wrote:

> At 10:18 AM 23/10/2001 +0200, Gerhard Häring wrote:
>
>> On Mon, Oct 22, 2001 at 10:51:02PM -0300, Lucio Torre wrote:
>> >  From the documentation:
>> >
>> > "The poll() system call, supported on most Unix systems"
>> >
>> > and my questions:
>> >
>> > a) is poll supported on Windows?
>> >
>> > my guess is no, but i hoping for a yes.
>>
>> It isn't. Unless, you're using Cygwin. Also, select only works with
>> sockets on Windows.
>>
>> Wouldn't it be possible to emulate poll() with select()? I'd be glad to
>> see a patch appear at Sourceforge if that's possible :-)
>>
>> Gerhard
>> -
>
>
> i think i could do that. i can make a .c with the code that amulates 
> poll from select, if someone else is willing to make it a patch or 
> whatever it should be to get into python.
>
> btw: is it worth it? poll is supposed to be faster, so why emulate it? 
> it would have no reason to exist!
>
>
>
As i wanted to use poll, but still be able to develop on my windows 
boxes, i did the following. Not the C version it could have been, but it 
helps me. Comments please.


import select
"""
Constant            Meaning
POLLIN              There is data to read
POLLPRI             There is urgent data to read
POLLOUT             Ready for output: writing will not block
POLLERR             Error condition of some sort
POLLHUP             Hung up
POLLNVAL            Invalid request: descriptor not open


TODO:
    POLLPRI
    POLLHUP
    POLLNVAL
"""

POLLIN=1        #   There is data to read
POLLPRI=2       #   There is urgent data to read
POLLOUT=4       #   Ready for output: writing will not block
POLLERR=8       #   Error condition of some sort
POLLHUP=16      #   Hung up
POLLNVAL=32     #   Invalid request: descriptor not open

class poll:
    def __init__(self):
        self.read_fds = []
        self.write_fds = []
        self.excep_fds = []
   
    def register(self, fd, events = POLLIN | POLLOUT | POLLPRI ):
        if events & POLLIN:
            if not fd in self.read_fds:
                self.read_fds.append(fd)
        if events & POLLPRI:
            if not fd in self.read_fds:
                self.read_fds.append(fd)
        if events & POLLOUT:
            if not fd in self.write_fds:
                self.write_fds.append(fd)
        if events & POLLERR:
            if not fd in self.excep_fds:
                self.excep_fds.append(fd)
        if events & POLLHUP:
            if not fd in self.excep_fds:
                self.excep_fds.append(fd)

    def unregister(self, fd):
        if fd in self.read_fds:
            self.read_fds.remove(fd)
        if fd in self.write_fds:
            self.write_fds.remove(fd)
        if fd in self.excep_fds:
            self.excep_fds.remove(fd)

    def poll(self, timeout = -1):
        if ( self.read_fds == [] and self.write_fds == [] and 
self.excep_fds == []):
            return ()
        if timeout != -1:
            result = select.select(self.read_fds, self.write_fds, 
self.excep_fds)
        else:
            result = select.select(self.read_fds, self.write_fds, 
self.excep_fds, timeout)

        print result
        ret = []
        for i in result[0]:
            ret.append((i, POLLIN))
        for i in result[1]:
            ret.append((i, POLLOUT))
        for i in result[0]:
            ret.append((i, POLLERR))

        return ret
       







More information about the Python-list mailing list