[Twisted-Python] _pollingfile._PollingTimer

Manlio Perillo manlio_perillo at libero.it
Sun Oct 1 14:51:41 EDT 2006

Jean-Paul Calderone ha scritto:
> On Sun, 01 Oct 2006 19:06:46 +0200, Manlio Perillo
> <manlio_perillo at libero.it> wrote:
>> Since the _PollingTimer can be reused for a possible console support,
>> why not to make it a public "simil reactor"?
>> As an example:
>> class UPollReactor(object):
>>   """User mode polling reactor, for Windows.
>>      Note that this is not a real reactor.
>>   """
>>   def addResource(self, resource, action):
>>     ...
>>   def run():
>>      # add a fake resource, just to do the job done by twistd with the
>>      # TimeService
>>      ...
>> twistd can, as an example, always "run" the UPollReactor when on a
>> windows system.
> Huh?

Forget about the run method.

I'm asking for an UPollreactor because I would like to do something like

class PollableReader(object):

    def __init__(self, handle, receivedCallback, lostCallback, reactor):
        self.handle = handle
        self.receivedCallback = receivedCallback
        self.lostCallback = lostCallback
        self.reactor = reactor

    def doWork(self):
        raise NotImplementedError()

    def close(self):
        except pywintypes.error:
            # You can't close std handles...?

    def stopProducing(self):

    def pauseProducing(self):

    def resumeProducing(self):

class Win32EventReactorWrapper(object):
   def __init__(self, reactor):
      self.reactor = reactor

   def addResource(self, rsc):
      reactor.addEvent(rsc, rsc.handle, "doWork")

   def removeResource(self, rsc):

Now I can do, as an example:

class ConsoleReader(PollableReader):
  # implements doWork for a console

class PipeReader(PollableReader):
  # implements doWork for a pipe


# assuming reactor has a name attribute
if reactor.name == "win32eventreactor"
    # do the polling in kernel space
    stdin = Console(..., Win32EventReactorWrapper(reactor))
    # do the polling in user space
    stdin = Console(..., upollreactor)

Console handles can be passed to WaitForSingleObject and friends.
They became signaled when input is avaliable.

This means that I can avoid to use the polling when the
win32eventreactor is being used, without having to modify the code.

I *really* believed the same was true for anonymous pipes, but this is
not the case...

Regards  Manlio Perillo

More information about the Twisted-Python mailing list