[Twisted-Python] _pollingfile._PollingTimer

Manlio Perillo manlio_perillo at libero.it
Sun Oct 1 12:51:41 MDT 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
this:

class PollableReader(object):
    implements(IPushProducer)

    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):
        try:
            win32api.CloseHandle(self.handle)
        except pywintypes.error:
            # You can't close std handles...?
            pass

    def stopProducing(self):
        self.close()

    def pauseProducing(self):
        self.reactor.addResource(self)

    def resumeProducing(self):
        self.reactor.removeResource(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):
      reactor.removeEvent(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))
else:
    # 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