twisted internet.reactor部分ソース分析
7972 ワード
twisted.internet.reactorはtwistedイベントのループをすべて行う場所です.
reactorは1つのpythonプロセスに1つしかありません.
Windowsの下で使うのはselectです.linux下epool.mac下はpoolですが、これはsocketserver、tornadoと同じですね().
ソースはtwisted.internet.default._getInstallFunction
SelectReactorを見て
主な機能はposixbaseで実現する.PosixReactorBaseというクラス.そしてosによって異なるマルチスレッドmixinが混入する.
各ReactorはIreactorFDSetのインタフェースを実装する.インタフェースは、ポートのステータスを管理する方法を定義します.例えばaddReader、addWriter.やはりコードを入れましょう.
変なところはありません.の
だからこっちを見ない.ここはよく知っている.マルチスレッドのMixinとポートreader、writerのインタフェースを除いて、残りはreactorの主要な部分です
次にposixbaseを見てみましょう.PosixReactorBaseというベースクラス.なぜReactorBaseではなくPosixReactorBaseなのか、後者は彼の親だからだ.
そしてついにこのReactorBase類を見て、糸を引いて繭を剥いて、最後に見たのはこれです.
ここではIreactorCore,IreactorTime,IreactorPluggableResolverの3つのインタフェースを実現する.(コアの2文字が顔に書いてあります.「顔を打つ」はここに向いています)
Ireactorは何を実現したのか.Reacorの主な特徴です.Event操作.ReactorのスイッチとDNSクエリー...BTW、これはBaseHTTPServerが考えているよりずっといいです.
後者はDNSを直接調べた.ここにはイベントループが組み込まれており,プロセスをブロックしていない.
IreactorTimeはdeferオブジェクト処理、seconds、callLaterなどについてです.
IreactorPluggableResolverは、DNSのクエリーに関するインタフェースを提供し、自分でキャッシュやプリセットDNS、ローカルDNSなどを実現するのに便利です.
ちょっと休んでください.
reactorは1つのpythonプロセスに1つしかありません.
Windowsの下で使うのはselectです.linux下epool.mac下はpoolですが、これはsocketserver、tornadoと同じですね().
ソースはtwisted.internet.default._getInstallFunction
try:
if platform.isLinux():
try:
from twisted.internet.epollreactor import install
except ImportError:
from twisted.internet.pollreactor import install
elif platform.getType() == 'posix' and not platform.isMacOSX():
from twisted.internet.pollreactor import install
else:
from twisted.internet.selectreactor import install
except ImportError:
from twisted.internet.selectreactor import install
return install
SelectReactorを見て
try:
from twisted.internet.win32eventreactor import _ThreadedWin32EventsMixin
except ImportError:
_extraBase = object
else:
_extraBase = _ThreadedWin32EventsMixin
@implementer(IReactorFDSet)
class SelectReactor(posixbase.PosixReactorBase, _extraBase):
"""
A select() based reactor - runs on all POSIX platforms and on Win32.
@ivar _reads: A set containing L{FileDescriptor} instances which will be
checked for read events.
@ivar _writes: A set containing L{FileDescriptor} instances which will be
checked for writability.
"""
balabala........
主な機能はposixbaseで実現する.PosixReactorBaseというクラス.そしてosによって異なるマルチスレッドmixinが混入する.
各ReactorはIreactorFDSetのインタフェースを実装する.インタフェースは、ポートのステータスを管理する方法を定義します.例えばaddReader、addWriter.やはりコードを入れましょう.
class IReactorFDSet(Interface):
"""
Implement me to be able to use L{IFileDescriptor} type resources.
This assumes that your main-loop uses UNIX-style numeric file descriptors
(or at least similarly opaque IDs returned from a .fileno() method)
"""
def addReader(reader):
"""
I add reader to the set of file descriptors to get read events for.
@param reader: An L{IReadDescriptor} provider that will be checked for
read events until it is removed from the reactor with
L{removeReader}.
@return: C{None}.
"""
def addWriter(writer):
"""
I add writer to the set of file descriptors to get write events for.
@param writer: An L{IWriteDescriptor} provider that will be checked for
write events until it is removed from the reactor with
L{removeWriter}.
@return: C{None}.
"""
def removeReader(reader):
"""
Removes an object previously added with L{addReader}.
@return: C{None}.
"""
def removeWriter(writer):
"""
Removes an object previously added with L{addWriter}.
@return: C{None}.
"""
def removeAll():
"""
Remove all readers and writers.
Should not remove reactor internal reactor connections (like a waker).
@return: A list of L{IReadDescriptor} and L{IWriteDescriptor} providers
which were removed.
"""
def getReaders():
"""
Return the list of file descriptors currently monitored for input
events by the reactor.
@return: the list of file descriptors monitored for input events.
@rtype: C{list} of C{IReadDescriptor}
"""
def getWriters():
"""
Return the list file descriptors currently monitored for output events
by the reactor.
@return: the list of file descriptors monitored for output events.
@rtype: C{list} of C{IWriteDescriptor}
"""
変なところはありません.の
だからこっちを見ない.ここはよく知っている.マルチスレッドのMixinとポートreader、writerのインタフェースを除いて、残りはreactorの主要な部分です
次にposixbaseを見てみましょう.PosixReactorBaseというベースクラス.なぜReactorBaseではなくPosixReactorBaseなのか、後者は彼の親だからだ.
@implementer(IReactorTCP, IReactorUDP, IReactorMulticast)
class PosixReactorBase(_SignalReactorMixin, _DisconnectSelectableMixin,
ReactorBase):
""" A basis for reactors that use file descriptors.
@ivar _childWaker: C{None} or a reference to the L{_SIGCHLDWaker}
which is used to properly notice child process termination.
"""
# Callable that creates a waker, overrideable so that subclasses can
# substitute their own implementation:
IReactorTCP, IReactorUDP, IReactorMulticast PosixReactorBase 。 , ListenSSL,listenUNIX,listenUNIXDatagram, adoptStreamPort IPV6。。he, 。
PosixReactorBase 。
1. _SignalReactorMixin, Reactor 。。
reactor stop run 。Reactor run 。。
2、_DisconnectSelectableMixin。 MixIn 。 haf_close, TCP 4 。 , ConnectionLost ConnectionDone.
そしてついにこのReactorBase類を見て、糸を引いて繭を剥いて、最後に見たのはこれです.
@implementer(IReactorCore, IReactorTime, IReactorPluggableResolver)
class ReactorBase(object):
"""
Default base class for Reactors.
@type _stopped: C{bool}
@ivar _stopped: A flag which is true between paired calls to C{reactor.run}
and C{reactor.stop}. This should be replaced with an explicit state
machine.
@type _justStopped: C{bool}
@ivar _justStopped: A flag which is true between the time C{reactor.stop}
is called and the time the shutdown system event is fired. This is
used to determine whether that event should be fired after each
iteration through the mainloop. This should be replaced with an
explicit state machine.
@type _started: C{bool}
@ivar _started: A flag which is true from the time C{reactor.run} is called
until the time C{reactor.run} returns. This is used to prevent calls
to C{reactor.run} on a running reactor. This should be replaced with
an explicit state machine.
@ivar running: See L{IReactorCore.running}
@ivar _registerAsIOThread: A flag controlling whether the reactor will
register the thread it is running in as the I/O thread when it starts.
If C{True}, registration will be done, otherwise it will not be.
"""
ここではIreactorCore,IreactorTime,IreactorPluggableResolverの3つのインタフェースを実現する.(コアの2文字が顔に書いてあります.「顔を打つ」はここに向いています)
Ireactorは何を実現したのか.Reacorの主な特徴です.Event操作.ReactorのスイッチとDNSクエリー...BTW、これはBaseHTTPServerが考えているよりずっといいです.
後者はDNSを直接調べた.ここにはイベントループが組み込まれており,プロセスをブロックしていない.
IreactorTimeはdeferオブジェクト処理、seconds、callLaterなどについてです.
IreactorPluggableResolverは、DNSのクエリーに関するインタフェースを提供し、自分でキャッシュやプリセットDNS、ローカルDNSなどを実現するのに便利です.
ちょっと休んでください.