Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/internet/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyc


BNc@sdZddlZddlZddlmZddlmZddlmZddl	m
Z
mZmZm
Z
mZyddl	mZWn9ek
rddlZejdd	ed
ZnXddlmZmZddlmZmZmZmZddlZdd
lmZddlmZm Z m!Z!ddl"m#Z#ddl"m$Z$ddl%m&Z&dej'fdYZ(de)fdYZ*de)fdYZ+dZ,ddgZ-dS(s
A win32event based implementation of the Twisted main loop.

This requires pywin32 (formerly win32all) or ActivePython to be installed.

To install the event loop (and you should do this before any connections,
listeners or connectors are added)::

    from twisted.internet import win32eventreactor
    win32eventreactor.install()

LIMITATIONS:
 1. WaitForMultipleObjects and thus the event loop can only handle 64 objects.
 2. Process running has some problems (see L{Process} docstring).


TODO:
 1. Event loop handling of writes is *very* problematic (this is causing failed tests).
    Switch to doing it the correct way, whatever that means (see below).
 2. Replace icky socket loopback waker with event based waker (use dummyEvent object)
 3. Switch everyone to using Free Software so we don't have to deal with proprietary APIs.


ALTERNATIVE SOLUTIONS:
 - IIRC, sockets can only be registered once. So we switch to a structure
   like the poll() reactor, thus allowing us to deal with write events in
   a decent fashion. This should allow us to pass tests, but we're still
   limited to 64 events.

Or:

 - Instead of doing a reactor, we make this an addon to the select reactor.
   The WFMO event loop runs in a separate thread. This means no need to maintain
   separate code for networking, 64 event limit doesn't apply to sockets,
   we can run processes and other win32 stuff in default event loop. The
   only problem is that we're stuck with the icky socket based waker.
   Another benefit is that this could be extended to support >64 events
   in a simpler manner than the previous solution.

The 2nd solution is probably what will get implemented.
iN(tThread(tWeakKeyDictionary(t
implements(tFD_READtFD_CLOSEt	FD_ACCEPTt
FD_CONNECTtWSAEventSelect(tWSAEnumNetworkEventssAReliable disconnection notification requires pywin32 215 or latertcategorycCs
ttgS(N(tsetR(tfdtevent((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyRAs(tCreateEventtMsgWaitForMultipleObjects(t
WAIT_OBJECT_0tWAIT_TIMEOUTtQS_ALLINPUTtQS_ALLEVENTS(t	posixbase(tlogt
threadabletfailure(t
IReactorFDSet(tIReactorWin32Events(tblockingCallFromThreadtWin32ReactorcBseZdZeeeeddddZdZ	dZ
dZdZdZ
dZdZd	Zd
ZdZdZd
ZdZdZeZRS(s
    Reactor that uses Win32 event APIs.

    @ivar _reads: A dictionary mapping L{FileDescriptor} instances to a
        win32 event object used to check for read events for that descriptor.

    @ivar _writes: A dictionary mapping L{FileDescriptor} instances to a
        arbitrary value.  Keys in this dictionary will be given a chance to
        write out their data.

    @ivar _events: A dictionary mapping win32 event object to tuples of
        L{FileDescriptor} instances and event masks.

    @ivar _closedAndReading: Along with C{_closedAndNotReading}, keeps track of
        descriptors which have had close notification delivered from the OS but
        which we have not finished reading data from.  MsgWaitForMultipleObjects
        will only deliver close notification to us once, so we remember it in
        these two dictionaries until we're ready to act on it.  The OS has
        delivered close notification for each descriptor in this dictionary, and
        the descriptors are marked as allowed to handle read events in the
        reactor, so they can be processed.  When a descriptor is marked as not
        allowed to handle read events in the reactor (ie, it is passed to
        L{IReactorFDSet.removeReader}), it is moved out of this dictionary and
        into C{_closedAndNotReading}.  The descriptors are keys in this
        dictionary.  The values are arbitrary.
    @type _closedAndReading: C{dict}

    @ivar _closedAndNotReading: These descriptors have had close notification
        delivered from the OS, but are not marked as allowed to handle read
        events in the reactor.  They are saved here to record their closed
        state, but not processed at all.  When one of these descriptors is
        passed to L{IReactorFDSet.addReader}, it is moved out of this dictionary
        and into C{_closedAndReading}.  The descriptors are keys in this
        dictionary.  The values are arbitrary.  This is a weak key dictionary so
        that if an application tells the reactor to stop reading from a
        descriptor and then forgets about that descriptor itself, the reactor
        will also forget about it.
    @type _closedAndNotReading: C{WeakKeyDictionary}
    icCsDi|_i|_i|_i|_t|_tjj|dS(N(	t_readst_writest_eventst_closedAndReadingRt_closedAndNotReadingRtPosixReactorBaset__init__(tself((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR!}s				cCs<tdddd}t|||||f|j|<|S(s9
        Make a win32 event object for a socket.
        iN(R
tNoneRR(R"RtactiontwhyR((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt_makeSocketEventscCs||f|j|<dS(s:
        Add a new win32 event to the event loop.
        N(R(R"RRR$((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pytaddEventscCs|j|=dS(s"
        Remove an event.
        N(R(R"R((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pytremoveEventscCsg||jkrc|j|dttBtBtB|j|<||jkrct|j|<|j|=qcndS(sY
        Add a socket FileDescriptor for notification of data available to read.
        tdoReadN(	RR&RRRRRtTrueR(R"treader((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt	addReaders"
cCs#||jkrd|j|<ndS(sZ
        Add a socket FileDescriptor for notification of data available to write.
        iN(R(R"twriter((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt	addWriterscCsZ||jkrV|j|j|=|j|=||jkrVt|j|<|j|=qVndS(sHRemove a Selectable for notification of data available to read.
        N(RRRR*R(R"R+((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pytremoveReaders

cCs ||jkr|j|=ndS(sIRemove a Selectable for notification of data available to write.
        N(R(R"R-((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pytremoveWriterscCs|j|j|jS(sD
        Remove all selectables, and return a list of them.
        (t
_removeAllRR(R"((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt	removeAllscCs
|jjS(N(Rtkeys(R"((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt
getReadersscCs
|jjS(N(RR3(R"((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt
getWritersscCs7tjddddd||dkr1d}nt}x-|jjD]}t}|jd|qGWx3|jjD]"}t}tj	||j
|qwW|rd}n|jp|jstj
|dS|jjp|jg}t|d	}t|d|ttB}|tkr#dS|tt|kretj}|r3|jd|jdSn|tkr3|tt|kr3||t}|j|\}}	||jkr|j}
|
d
kr|j|tjtdSt|
|}t |krt|j|<qntj	||j|	|ndS(NtchanneltsystemRt	iterationtreactoridR)iii(!RtmsgR#tFalseRR3R*t
_runActionRtcallWithLoggert	_runWriteRttimetsleept
dummyEventtintRRRRRtlentwin32guitPumpWaitingMessagest	callLatertstopRtfilenot_disconnectSelectableRt_NO_FILEDESCRR(R"ttimeouttranUserCodeR+RthandlestvaltexitRR$RHtevents((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pytdoWaitForMultipleEventssJ		
"cCsd}y|j}Wn!tjd}tjnX|r|j||j|y|jtj	|WqtjqXn|dkrdSdS(Nii(tdoWritetsystexc_infoRtdeferrR/R0tconnectionLostRtFailureR#(R"Rtclosed((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR>s

cCs`yt||}Wn!tjd}tjnX|r\|j|||dkndS(NiR)(tgetattrRSRTRRURI(R"R$RRX((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR<+sN(t__name__t
__module__t__doc__RRRR
R#RAR!R&R'R(R,R.R/R0R2R4R5RQR>R<tdoIteration(((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyRQs$'
			
										A			t_ThreadFDWrappercBs2eZdZdZdZdZdZRS(s7
    This wraps an event handler and translates notification in the helper
    L{Win32Reactor} thread into a notification in the primary reactor thread.

    @ivar _reactor: The primary reactor, the one to which event notification
        will be sent.

    @ivar _fd: The L{FileDescriptor} to which the event will be dispatched.

    @ivar _action: A C{str} giving the method of C{_fd} which handles the event.

    @ivar _logPrefix: The pre-fetched log prefix string for C{_fd}, so that
        C{_fd.logPrefix} does not need to be called in a non-main thread.
    cCs(||_||_||_||_dS(N(t_reactort_fdt_actiont
_logPrefix(R"R9RR$t	logPrefix((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR!Gs			cCs|jS(sc
        Return the original handler's log prefix, as it was given to
        C{__init__}.
        (Rb(R"((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyRcNscstjfdS(s-
        Callback fired when the associated event is set.  Run the C{action}
        callback on the wrapped descriptor in the main reactor thread and raise
        or return whatever it raises or returns to cause this event handler to
        be removed from C{self._reactor} if appropriate.
        cstjjS(N(RYR`Ra((R"(sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt<lambda>^s(RR_(R"((R"sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt_executeVscCs|jj|jj|dS(s
        Pass through to the wrapped descriptor, but in the main reactor thread
        instead of the helper C{Win32Reactor} thread.
        N(R_tcallFromThreadR`RV(R"treason((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyRVas(RZR[R\R!RcReRV(((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR^8s
			t_ThreadedWin32EventsMixincBsHeZdZeedZdZdZdZ	dZ
dZRS(s
    This mixin implements L{IReactorWin32Events} for another reactor by running
    a L{Win32Reactor} in a separate thread and dispatching work to it.

    @ivar _reactor: The L{Win32Reactor} running in the other thread.  This is
        C{None} until it is actually needed.

    @ivar _reactorThread: The L{threading.Thread} which is running the
        L{Win32Reactor}.  This is C{None} until it is actually needed.
    cCs#|jj|jjd|_dS(sP
        Stop and discard the reactor started by C{_makeHelperReactor}.
        N(R_RfRGR#(R"((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt_unmakeHelperReactor{scCs`t|_t|j_td|jjdtf|_|jdd|j|jj	dS(s
        Create and (in a new thread) start a L{Win32Reactor} instance to use for
        the implementation of L{IReactorWin32Events}.
        ttargettargstaftertshutdownN(
RR_R;t_registerAsIOThreadRtrunt_reactorThreadtaddSystemEventTriggerRitstart(R"((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt_makeHelperReactorscCsT|jdkr|jn|jj|jj|t||||jddS(s.
        @see: L{IReactorWin32Events}
        ReN(R_R#RsRfR'R^Rc(R"RRR$((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR's
		cCs|jj|jj|dS(s.
        @see: L{IReactorWin32Events}
        N(R_RfR((R"R((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyR(sN(RZR[R\RRR#R_RpRiRsR'R((((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyRhjs

			cCs3tjdt}ddl}|j|dS(Nii(RtinitRtmaintinstallReactor(trRu((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pytinstalls
	Rx(.R\R?RSt	threadingRtweakrefRtzope.interfaceRt	win32fileRRRRRRtImportErrortwarningstwarntUserWarningt
win32eventR
RRRRRRDttwisted.internetRttwisted.pythonRRRttwisted.internet.interfacesRRttwisted.internet.threadsRR RtobjectR^RhRxt__all__(((sF/usr/lib/python2.7/dist-packages/twisted/internet/win32eventreactor.pyt<module>-s6(

"2=