Esempio n. 1
0
def install_reactor(use_asyncio=False):
    """
    Borrowed from https://github.com/crossbario/autobahn-python/blob/master/autobahn/twisted/choosereactor.py
    """
    current_reactor = current_reactor_klass()    
    if current_reactor:
        return current_reactor

    if use_asyncio:
        #files=132, cost=186.99198293685913 seconds, speed=2007542.4309862417
        import asyncio
        import uvloop
        #asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
        asyncio.set_event_loop(uvloop.new_event_loop())
        from twisted.internet import asyncioreactor
        asyncioreactor.install()
    elif 'bsd' in sys.platform or sys.platform.startswith('darwin'):
        # This reactor is faster in MacOS
        # files=132, cost=61.64284586906433 seconds, speed=6089828.182127992
        # files=132, cost=58.344452142715454 seconds, speed=6434105.149907892
        # *BSD and MacOSX
        #
        from twisted.internet import kqreactor
        kqreactor.install()
    elif sys.platform in ['win32']:
        from twisted.internet.iocpreactor import reactor as iocpreactor
        iocpreactor.install()
    elif sys.platform.startswith('linux'):
        from twisted.internet import epollreactor
        epollreactor.install()
    else:
        from twisted.internet import selectreactor
        selectreactor.install()
    from twisted.internet import reactor
    return reactor
Esempio n. 2
0
def install_optimal_reactor(verbose = False):
    """
    Try to install the optimal Twisted reactor for platform.
    
    :param verbose: If ``True``, print what happens.
    :type verbose: bool
    """
    import sys
    from twisted.python import reflect
    if 'twisted.internet.reactor' in sys.modules:
        current_reactor = reflect.qual(sys.modules['twisted.internet.reactor'].__class__).split('.')[-1]
    else:
        current_reactor = None
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):
        if current_reactor != 'KQueueReactor':
            try:
                v = sys.version_info
                if v[0] == 1 or v[0] == 2 and v[1] < 6 or v[0] == 2 and v[1] == 6 and v[2] < 5:
                    raise Exception('Python version too old ({0}) to use kqueue reactor'.format(sys.version))
                from twisted.internet import kqreactor
                kqreactor.install()
            except Exception as e:
                print 'WARNING: Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor ({0}).'.format(e)
            else:
                if verbose:
                    print 'Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.'
        elif verbose:
            print 'Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.'
    elif sys.platform in ('win32',):
        if current_reactor != 'IOCPReactor':
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor
                iocpreactor.install()
            except Exception as e:
                print 'WARNING: Running on Windows, but cannot install IOCP Twisted reactor ({0}).'.format(e)
            else:
                if verbose:
                    print 'Running on Windows and optimal reactor (ICOP) was installed.'
        elif verbose:
            print 'Running on Windows and optimal reactor (ICOP) already installed.'
    elif sys.platform.startswith('linux'):
        if current_reactor != 'EPollReactor':
            try:
                from twisted.internet import epollreactor
                epollreactor.install()
            except Exception as e:
                print 'WARNING: Running on Linux, but cannot install Epoll Twisted reactor ({0}).'.format(e)
            else:
                if verbose:
                    print 'Running on Linux and optimal reactor (epoll) was installed.'
        elif verbose:
            print 'Running on Linux and optimal reactor (epoll) already installed.'
    else:
        try:
            from twisted.internet import default as defaultreactor
            defaultreactor.install()
        except Exception as e:
            print 'WARNING: Could not install default Twisted reactor for this platform ({0}).'.format(e)

    return
Esempio n. 3
0
def run_twisted(host, port, barrier, profile):

    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):
        from twisted.internet import kqreactor
        kqreactor.install()
    elif sys.platform in ['win32']:
        from twisted.internet.iocpreactor import reactor as iocpreactor
        iocpreactor.install()
    elif sys.platform.startswith('linux'):
        from twisted.internet import epollreactor
        epollreactor.install()
    else:
        from twisted.internet import default as defaultreactor
        defaultreactor.install()

    from twisted.web.server import Site
    from twisted.web.resource import Resource
    from twisted.internet import reactor

    class TestResource(Resource):

        def __init__(self, name):
            super().__init__()
            self.name = name
            self.isLeaf = name is not None

        def render_GET(self, request):
            txt = 'Hello, ' + self.name
            request.setHeader(b'Content-Type', b'text/plain; charset=utf-8')
            return txt.encode('utf8')

        def getChild(self, name, request):
            return TestResource(name=name.decode('utf-8'))

    class PrepareResource(Resource):

        isLeaf = True

        def render_GET(self, request):
            gc.collect()
            return b'OK'

    class StopResource(Resource):

        isLeaf = True

        def render_GET(self, request):
            reactor.callLater(0.1, reactor.stop)
            return b'OK'

    root = Resource()
    root.putChild(b'test', TestResource(None))
    root.putChild(b'prepare', PrepareResource())
    root.putChild(b'stop', StopResource())
    site = Site(root)
    reactor.listenTCP(port, site, interface=host)
    barrier.wait()

    reactor.run()
Esempio n. 4
0
def run_twisted(host, port, barrier, profile):

    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):
        from twisted.internet import kqreactor
        kqreactor.install()
    elif sys.platform in ['win32']:
        from twisted.internet.iocpreactor import reactor as iocpreactor
        iocpreactor.install()
    elif sys.platform.startswith('linux'):
        from twisted.internet import epollreactor
        epollreactor.install()
    else:
        from twisted.internet import default as defaultreactor
        defaultreactor.install()

    from twisted.web.server import Site
    from twisted.web.resource import Resource
    from twisted.internet import reactor

    class TestResource(Resource):

        def __init__(self, name):
            super().__init__()
            self.name = name
            self.isLeaf = name is not None

        def render_GET(self, request):
            txt = 'Hello, ' + self.name
            request.setHeader(b'Content-Type', b'text/plain; charset=utf-8')
            return txt.encode('utf8')

        def getChild(self, name, request):
            return TestResource(name=name.decode('utf-8'))

    class PrepareResource(Resource):

        isLeaf = True

        def render_GET(self, request):
            gc.collect()
            return b'OK'

    class StopResource(Resource):

        isLeaf = True

        def render_GET(self, request):
            reactor.callLater(0.1, reactor.stop)
            return b'OK'

    root = Resource()
    root.putChild(b'test', TestResource(None))
    root.putChild(b'prepare', PrepareResource())
    root.putChild(b'stop', StopResource())
    site = Site(root)
    reactor.listenTCP(port, site, interface=host)
    barrier.wait()

    reactor.run()
Esempio n. 5
0
def install_twisted_reactor():
    """
    Install the Twisted reactor designed for this platform.
    """

    import sys

    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):
         from twisted.internet import kqreactor
         kqreactor.install()
    elif sys.platform in ['win32']:
         from twisted.internet.iocpreactor import reactor as iocpreactor
         iocpreactor.install()
    elif sys.platform.startswith('linux'):
         from twisted.internet import epollreactor
         epollreactor.install()
    else:
         from twisted.internet import default as defaultreactor
         defaultreactor.install()

    from twisted.internet import reactor
    
    return reactor
Esempio n. 6
0
def install_optimal_reactor(verbose=False):
    """
    Try to install the optimal Twisted reactor for platform.

    :param verbose: If ``True``, print what happens.
    :type verbose: bool
    """
    import sys
    from twisted.python import reflect
    import txaio
    txaio.use_twisted()  # just to be sure...
    # XXX should I configure txaio.config.loop in here too, or just in
    # install_reactor()? (I am: see bottom of function)

    # determine currently installed reactor, if any
    ##
    if 'twisted.internet.reactor' in sys.modules:
        current_reactor = reflect.qual(
            sys.modules['twisted.internet.reactor'].__class__).split('.')[-1]
    else:
        current_reactor = None

    # depending on platform, install optimal reactor
    ##
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):

        # *BSD and MacOSX
        ##
        if current_reactor != 'KQueueReactor':
            try:
                v = sys.version_info
                if v[0] == 1 or (v[0] == 2
                                 and v[1] < 6) or (v[0] == 2 and v[1] == 6
                                                   and v[2] < 5):
                    raise Exception(
                        "Python version too old ({0}) to use kqueue reactor".
                        format(sys.version))
                from twisted.internet import kqreactor
                kqreactor.install()
            except Exception as e:
                print(
                    "WARNING: Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor ({0})."
                    .format(e))
            else:
                if verbose:
                    print(
                        "Running on *BSD or MacOSX and optimal reactor (kqueue) was installed."
                    )
        else:
            if verbose:
                print(
                    "Running on *BSD or MacOSX and optimal reactor (kqueue) already installed."
                )

    elif sys.platform in ['win32']:

        # Windows
        ##
        if current_reactor != 'IOCPReactor':
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor
                iocpreactor.install()
            except Exception as e:
                print(
                    "WARNING: Running on Windows, but cannot install IOCP Twisted reactor ({0})."
                    .format(e))
            else:
                if verbose:
                    print(
                        "Running on Windows and optimal reactor (ICOP) was installed."
                    )
        else:
            if verbose:
                print(
                    "Running on Windows and optimal reactor (ICOP) already installed."
                )

    elif sys.platform.startswith('linux'):

        # Linux
        ##
        if current_reactor != 'EPollReactor':
            try:
                from twisted.internet import epollreactor
                epollreactor.install()
            except Exception as e:
                print(
                    "WARNING: Running on Linux, but cannot install Epoll Twisted reactor ({0})."
                    .format(e))
            else:
                if verbose:
                    print(
                        "Running on Linux and optimal reactor (epoll) was installed."
                    )
        else:
            if verbose:
                print(
                    "Running on Linux and optimal reactor (epoll) already installed."
                )

    else:
        try:
            from twisted.internet import default as defaultreactor
            defaultreactor.install()
        except Exception as e:
            print(
                "WARNING: Could not install default Twisted reactor for this platform ({0})."
                .format(e))

    from twisted.internet import reactor
    txaio.config.loop = reactor
def install_optimal_reactor(require_optimal_reactor=True):
    """
    Try to install the optimal Twisted reactor for this platform:

    - Linux:   epoll
    - BSD/OSX: kqueue
    - Windows: iocp
    - Other:   select

    Notes:

    - This function exists, because the reactor types selected based on platform
      in `twisted.internet.default` are different from here.
    - The imports are inlined, because the Twisted code base is notorious for
      importing the reactor as a side-effect of merely importing. Hence we postpone
      all importing.

    See: http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html#reactor-functionality

    :param require_optimal_reactor: If ``True`` and the desired reactor could not be installed,
        raise ``ReactorAlreadyInstalledError``, else fallback to another reactor.
    :type require_optimal_reactor: bool

    :returns: The Twisted reactor in place (`twisted.internet.reactor`).
    """
    log = txaio.make_logger()

    # determine currently installed reactor, if any
    #
    current_reactor = current_reactor_klass()

    # depending on platform, install optimal reactor
    #
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):

        # *BSD and MacOSX
        #
        if current_reactor != 'KQueueReactor':
            if current_reactor is None:
                try:
                    from twisted.internet import kqreactor
                    kqreactor.install()
                except:
                    log.warn('Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor: {tb}', tb=traceback.format_exc())
                else:
                    log.debug('Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.')
            else:
                log.warn('Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor, because another reactor ({klass}) is already installed.', klass=current_reactor)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug('Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.')

    elif sys.platform in ['win32']:

        # Windows
        #
        if current_reactor != 'IOCPReactor':
            if current_reactor is None:
                try:
                    from twisted.internet.iocpreactor import reactor as iocpreactor
                    iocpreactor.install()
                except:
                    log.warn('Running on Windows, but cannot install IOCP Twisted reactor: {tb}', tb=traceback.format_exc())
                else:
                    log.debug('Running on Windows and optimal reactor (ICOP) was installed.')
            else:
                log.warn('Running on Windows, but cannot install IOCP Twisted reactor, because another reactor ({klass}) is already installed.', klass=current_reactor)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug('Running on Windows and optimal reactor (ICOP) already installed.')

    elif sys.platform.startswith('linux'):

        # Linux
        #
        if current_reactor != 'EPollReactor':
            if current_reactor is None:
                try:
                    from twisted.internet import epollreactor
                    epollreactor.install()
                except:
                    log.warn('Running on Linux, but cannot install Epoll Twisted reactor: {tb}', tb=traceback.format_exc())
                else:
                    log.debug('Running on Linux and optimal reactor (epoll) was installed.')
            else:
                log.warn('Running on Linux, but cannot install Epoll Twisted reactor, because another reactor ({klass}) is already installed.', klass=current_reactor)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug('Running on Linux and optimal reactor (epoll) already installed.')

    else:

        # Other platform
        #
        if current_reactor != 'SelectReactor':
            if current_reactor is None:
                try:
                    from twisted.internet import selectreactor
                    selectreactor.install()
                    # from twisted.internet import default as defaultreactor
                    # defaultreactor.install()
                except:
                    log.warn('Running on "{platform}", but cannot install Select Twisted reactor: {tb}', tb=traceback.format_exc(), platform=sys.platform)
                else:
                    log.debug('Running on "{platform}" and optimal reactor (Select) was installed.', platform=sys.platform)
            else:
                log.warn('Running on "{platform}", but cannot install Select Twisted reactor, because another reactor ({klass}) is already installed.', klass=current_reactor, platform=sys.platform)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug('Running on "{platform}" and optimal reactor (Select) already installed.', platform=sys.platform)

    from twisted.internet import reactor
    txaio.config.loop = reactor

    return reactor
Esempio n. 8
0
def install_optimal_reactor(verbose=False):
    """
    Try to install the optimal Twisted reactor for this platform.

    :param verbose: If ``True``, print what happens.
    :type verbose: bool
    """
    log = make_logger()

    import sys
    from twisted.python import reflect
    import txaio
    txaio.use_twisted()  # just to be sure...
    # XXX should I configure txaio.config.loop in here too, or just in
    # install_reactor()? (I am: see bottom of function)

    # determine currently installed reactor, if any
    ##
    if 'twisted.internet.reactor' in sys.modules:
        current_reactor = reflect.qual(
            sys.modules['twisted.internet.reactor'].__class__).split('.')[-1]
    else:
        current_reactor = None

    # depending on platform, install optimal reactor
    ##
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):

        # *BSD and MacOSX
        ##
        if current_reactor != 'KQueueReactor':
            try:
                from twisted.internet import kqreactor
                kqreactor.install()
            except:
                log.critical(
                    "Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor"
                )
                log.debug(traceback.format_exc())
            else:
                log.debug(
                    "Running on *BSD or MacOSX and optimal reactor (kqueue) was installed."
                )
        else:
            log.debug(
                "Running on *BSD or MacOSX and optimal reactor (kqueue) already installed."
            )

    elif sys.platform in ['win32']:

        # Windows
        ##
        if current_reactor != 'IOCPReactor':
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor
                iocpreactor.install()
            except:
                log.critical(
                    "Running on Windows, but cannot install IOCP Twisted reactor"
                )
                log.debug(traceback.format_exc())
            else:
                log.debug(
                    "Running on Windows and optimal reactor (ICOP) was installed."
                )
        else:
            log.debug(
                "Running on Windows and optimal reactor (ICOP) already installed."
            )

    elif sys.platform.startswith('linux'):

        # Linux
        ##
        if current_reactor != 'EPollReactor':
            try:
                from twisted.internet import epollreactor
                epollreactor.install()
            except:
                log.critical(
                    "Running on Linux, but cannot install Epoll Twisted reactor"
                )
                log.debug(traceback.format_exc())
            else:
                log.debug(
                    "Running on Linux and optimal reactor (epoll) was installed."
                )
        else:
            log.debug(
                "Running on Linux and optimal reactor (epoll) already installed."
            )

    else:
        try:
            from twisted.internet import default as defaultreactor
            defaultreactor.install()
        except:
            log.critical(
                "Could not install default Twisted reactor for this platform")
            log.debug(traceback.format_exc())

    from twisted.internet import reactor
    txaio.config.loop = reactor
def install_optimal_reactor(verbose=False):
    """
    Try to install the optimal Twisted reactor for this platform.

    :param verbose: If ``True``, print what happens.
    :type verbose: bool
    """
    log = make_logger()

    import sys
    from twisted.python import reflect
    import txaio
    txaio.use_twisted()  # just to be sure...
    # XXX should I configure txaio.config.loop in here too, or just in
    # install_reactor()? (I am: see bottom of function)

    # determine currently installed reactor, if any
    ##
    if 'twisted.internet.reactor' in sys.modules:
        current_reactor = reflect.qual(sys.modules['twisted.internet.reactor'].__class__).split('.')[-1]
    else:
        current_reactor = None

    # depending on platform, install optimal reactor
    ##
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):

        # *BSD and MacOSX
        ##
        if current_reactor != 'KQueueReactor':
            try:
                from twisted.internet import kqreactor
                kqreactor.install()
            except:
                log.critical("Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor")
                log.debug(traceback.format_exc())
            else:
                log.debug("Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.")
        else:
            log.debug("Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.")

    elif sys.platform in ['win32']:

        # Windows
        ##
        if current_reactor != 'IOCPReactor':
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor
                iocpreactor.install()
            except:
                log.critical("Running on Windows, but cannot install IOCP Twisted reactor")
                log.debug(traceback.format_exc())
            else:
                log.debug("Running on Windows and optimal reactor (ICOP) was installed.")
        else:
            log.debug("Running on Windows and optimal reactor (ICOP) already installed.")

    elif sys.platform.startswith('linux'):

        # Linux
        ##
        if current_reactor != 'EPollReactor':
            try:
                from twisted.internet import epollreactor
                epollreactor.install()
            except:
                log.critical("Running on Linux, but cannot install Epoll Twisted reactor")
                log.debug(traceback.format_exc())
            else:
                log.debug("Running on Linux and optimal reactor (epoll) was installed.")
        else:
            log.debug("Running on Linux and optimal reactor (epoll) already installed.")

    else:
        try:
            from twisted.internet import default as defaultreactor
            defaultreactor.install()
        except:
            log.critical("Could not install default Twisted reactor for this platform")
            log.debug(traceback.format_exc())

    from twisted.internet import reactor
    txaio.config.loop = reactor
Esempio n. 10
0
# https://github.com/pyinstaller/pyinstaller/issues/3390

import sys

# this creates module: sys.modules['twisted.internet.reactor']
if sys.platform in ['win32']:
    from twisted.internet.iocpreactor import reactor as _iocpreactor
    _iocpreactor.install()
else:
    from twisted.internet import default
    default.install()
Esempio n. 11
0
def install_optimal_reactor(verbose=False):
    """
   Try to install the optimal Twisted reactor for platform.

   :param verbose: If ``True``, print what happens.
   :type verbose: bool
   """
    import sys
    from twisted.python import reflect
    from twisted.python import log

    ## determine currently installed reactor, if any
    ##
    if "twisted.internet.reactor" in sys.modules:
        current_reactor = reflect.qual(sys.modules["twisted.internet.reactor"].__class__).split(".")[-1]
    else:
        current_reactor = None

    ## depending on platform, install optimal reactor
    ##
    if "bsd" in sys.platform or sys.platform.startswith("darwin"):

        ## *BSD and MacOSX
        ##
        if current_reactor != "KQueueReactor":
            try:
                v = sys.version_info
                if v[0] == 1 or (v[0] == 2 and v[1] < 6) or (v[0] == 2 and v[1] == 6 and v[2] < 5):
                    raise Exception("Python version too old ({0}) to use kqueue reactor".format(sys.version))
                from twisted.internet import kqreactor

                kqreactor.install()
            except Exception as e:
                log.err(
                    "WARNING: Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor ({0}).".format(e)
                )
            else:
                if verbose:
                    log.msg("Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.")
        else:
            if verbose:
                log.msg("Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.")

    elif sys.platform in ["win32"]:

        ## Windows
        ##
        if current_reactor != "IOCPReactor":
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor

                iocpreactor.install()
            except Exception as e:
                log.err("WARNING: Running on Windows, but cannot install IOCP Twisted reactor ({0}).".format(e))
            else:
                if verbose:
                    log.msg("Running on Windows and optimal reactor (ICOP) was installed.")
        else:
            if verbose:
                log.msg("Running on Windows and optimal reactor (ICOP) already installed.")

    elif sys.platform.startswith("linux"):

        ## Linux
        ##
        if current_reactor != "EPollReactor":
            try:
                from twisted.internet import epollreactor

                epollreactor.install()
            except Exception as e:
                log.err("WARNING: Running on Linux, but cannot install Epoll Twisted reactor ({0}).".format(e))
            else:
                if verbose:
                    log.msg("Running on Linux and optimal reactor (epoll) was installed.")
        else:
            if verbose:
                log.msg("Running on Linux and optimal reactor (epoll) already installed.")

    else:
        try:
            from twisted.internet import default as defaultreactor

            defaultreactor.install()
        except Exception as e:
            log.err("WARNING: Could not install default Twisted reactor for this platform ({0}).".format(e))
Esempio n. 12
0
def install_optimal_reactor(verbose=False):
    """
    Try to install the optimal Twisted reactor for this platform.

    :param verbose: If ``True``, print what happens.
    :type verbose: bool
    """
    log = txaio.make_logger()

    import sys
    from twisted.python import reflect

    # determine currently installed reactor, if any
    ##
    if "twisted.internet.reactor" in sys.modules:
        current_reactor = reflect.qual(sys.modules["twisted.internet.reactor"].__class__).split(".")[-1]
    else:
        current_reactor = None

    # depending on platform, install optimal reactor
    ##
    if "bsd" in sys.platform or sys.platform.startswith("darwin"):

        # *BSD and MacOSX
        ##
        if current_reactor != "KQueueReactor":
            try:
                from twisted.internet import kqreactor

                kqreactor.install()
            except:
                log.critical("Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor")
                log.warn("{tb}", tb=traceback.format_exc())
            else:
                log.debug("Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.")
        else:
            log.debug("Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.")

    elif sys.platform in ["win32"]:

        # Windows
        ##
        if current_reactor != "IOCPReactor":
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor

                iocpreactor.install()
            except:
                log.critical("Running on Windows, but cannot install IOCP Twisted reactor")
                log.warn("{tb}", tb=traceback.format_exc())
            else:
                log.debug("Running on Windows and optimal reactor (ICOP) was installed.")
        else:
            log.debug("Running on Windows and optimal reactor (ICOP) already installed.")

    elif sys.platform.startswith("linux"):

        # Linux
        ##
        if current_reactor != "EPollReactor":
            try:
                from twisted.internet import epollreactor

                epollreactor.install()
            except:
                log.critical("Running on Linux, but cannot install Epoll Twisted reactor")
                log.warn("{tb}", tb=traceback.format_exc())
            else:
                log.debug("Running on Linux and optimal reactor (epoll) was installed.")
        else:
            log.debug("Running on Linux and optimal reactor (epoll) already installed.")

    else:
        try:
            from twisted.internet import default as defaultreactor

            defaultreactor.install()
        except:
            log.critical("Could not install default Twisted reactor for this platform")
            log.warn("{tb}", tb=traceback.format_exc())

    from twisted.internet import reactor

    txaio.config.loop = reactor
Esempio n. 13
0
def install_optimal_reactor(require_optimal_reactor=True):
    """
    Try to install the optimal Twisted reactor for this platform:

    - Linux:   epoll
    - BSD/OSX: kqueue
    - Windows: iocp
    - Other:   select

    Notes:

    - This function exists, because the reactor types selected based on platform
      in `twisted.internet.default` are different from here.
    - The imports are inlined, because the Twisted code base is notorious for
      importing the reactor as a side-effect of merely importing. Hence we postpone
      all importing.

    See: http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html#reactor-functionality

    :param require_optimal_reactor: If ``True`` and the desired reactor could not be installed,
        raise ``ReactorAlreadyInstalledError``, else fallback to another reactor.
    :type require_optimal_reactor: bool

    :returns: The Twisted reactor in place (`twisted.internet.reactor`).
    """
    log = txaio.make_logger()

    # determine currently installed reactor, if any
    #
    current_reactor = current_reactor_klass()

    # depending on platform, install optimal reactor
    #
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):

        # *BSD and MacOSX
        #
        if current_reactor != 'KQueueReactor':
            if current_reactor is None:
                try:
                    from twisted.internet import kqreactor
                    kqreactor.install()
                except:
                    log.warn(
                        'Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor: {tb}',
                        tb=traceback.format_exc())
                else:
                    log.debug(
                        'Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.'
                    )
            else:
                log.warn(
                    'Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor, because another reactor ({klass}) is already installed.',
                    klass=current_reactor)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug(
                'Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.'
            )

    elif sys.platform in ['win32']:

        # Windows
        #
        if current_reactor != 'IOCPReactor':
            if current_reactor is None:
                try:
                    from twisted.internet.iocpreactor import reactor as iocpreactor
                    iocpreactor.install()
                except:
                    log.warn(
                        'Running on Windows, but cannot install IOCP Twisted reactor: {tb}',
                        tb=traceback.format_exc())
                else:
                    log.debug(
                        'Running on Windows and optimal reactor (ICOP) was installed.'
                    )
            else:
                log.warn(
                    'Running on Windows, but cannot install IOCP Twisted reactor, because another reactor ({klass}) is already installed.',
                    klass=current_reactor)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug(
                'Running on Windows and optimal reactor (ICOP) already installed.'
            )

    elif sys.platform.startswith('linux'):

        # Linux
        #
        if current_reactor != 'EPollReactor':
            if current_reactor is None:
                try:
                    from twisted.internet import epollreactor
                    epollreactor.install()
                except:
                    log.warn(
                        'Running on Linux, but cannot install Epoll Twisted reactor: {tb}',
                        tb=traceback.format_exc())
                else:
                    log.debug(
                        'Running on Linux and optimal reactor (epoll) was installed.'
                    )
            else:
                log.warn(
                    'Running on Linux, but cannot install Epoll Twisted reactor, because another reactor ({klass}) is already installed.',
                    klass=current_reactor)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug(
                'Running on Linux and optimal reactor (epoll) already installed.'
            )

    else:

        # Other platform
        #
        if current_reactor != 'SelectReactor':
            if current_reactor is None:
                try:
                    from twisted.internet import selectreactor
                    selectreactor.install()
                    # from twisted.internet import default as defaultreactor
                    # defaultreactor.install()
                except:
                    log.warn(
                        'Running on "{platform}", but cannot install Select Twisted reactor: {tb}',
                        tb=traceback.format_exc(),
                        platform=sys.platform)
                else:
                    log.debug(
                        'Running on "{platform}" and optimal reactor (Select) was installed.',
                        platform=sys.platform)
            else:
                log.warn(
                    'Running on "{platform}", but cannot install Select Twisted reactor, because another reactor ({klass}) is already installed.',
                    klass=current_reactor,
                    platform=sys.platform)
                if require_optimal_reactor:
                    raise ReactorAlreadyInstalledError()
        else:
            log.debug(
                'Running on "{platform}" and optimal reactor (Select) already installed.',
                platform=sys.platform)

    from twisted.internet import reactor
    txaio.config.loop = reactor

    return reactor
Esempio n. 14
0
def install_optimal_reactor(verbose=False):
    """
    Try to install the optimal Twisted reactor for platform.

    :param verbose: If ``True``, print what happens.
    :type verbose: bool
    """
    import sys
    from twisted.python import reflect
    import txaio
    txaio.use_twisted()  # just to be sure...
    # XXX should I configure txaio.config.loop in here too, or just in
    # install_reactor()? (I am: see bottom of function)

    # determine currently installed reactor, if any
    ##
    if 'twisted.internet.reactor' in sys.modules:
        current_reactor = reflect.qual(sys.modules['twisted.internet.reactor'].__class__).split('.')[-1]
    else:
        current_reactor = None

    # depending on platform, install optimal reactor
    ##
    if 'bsd' in sys.platform or sys.platform.startswith('darwin'):

        # *BSD and MacOSX
        ##
        if current_reactor != 'KQueueReactor':
            try:
                v = sys.version_info
                if v[0] == 1 or (v[0] == 2 and v[1] < 6) or (v[0] == 2 and v[1] == 6 and v[2] < 5):
                    raise Exception("Python version too old ({0}) to use kqueue reactor".format(sys.version))
                from twisted.internet import kqreactor
                kqreactor.install()
            except Exception as e:
                print("WARNING: Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor ({0}).".format(e))
            else:
                if verbose:
                    print("Running on *BSD or MacOSX and optimal reactor (kqueue) was installed.")
        else:
            if verbose:
                print("Running on *BSD or MacOSX and optimal reactor (kqueue) already installed.")

    elif sys.platform in ['win32']:

        # Windows
        ##
        if current_reactor != 'IOCPReactor':
            try:
                from twisted.internet.iocpreactor import reactor as iocpreactor
                iocpreactor.install()
            except Exception as e:
                print("WARNING: Running on Windows, but cannot install IOCP Twisted reactor ({0}).".format(e))
            else:
                if verbose:
                    print("Running on Windows and optimal reactor (ICOP) was installed.")
        else:
            if verbose:
                print("Running on Windows and optimal reactor (ICOP) already installed.")

    elif sys.platform.startswith('linux'):

        # Linux
        ##
        if current_reactor != 'EPollReactor':
            try:
                from twisted.internet import epollreactor
                epollreactor.install()
            except Exception as e:
                print("WARNING: Running on Linux, but cannot install Epoll Twisted reactor ({0}).".format(e))
            else:
                if verbose:
                    print("Running on Linux and optimal reactor (epoll) was installed.")
        else:
            if verbose:
                print("Running on Linux and optimal reactor (epoll) already installed.")

    else:
        try:
            from twisted.internet import default as defaultreactor
            defaultreactor.install()
        except Exception as e:
            print("WARNING: Could not install default Twisted reactor for this platform ({0}).".format(e))

    from twisted.internet import reactor
    txaio.config.loop = reactor
Esempio n. 15
0
import sys
import ujson

if 'bsd' in sys.platform or sys.platform.startswith('darwin'):
    from twisted.internet import kqreactor
    kqreactor.install()
elif sys.platform in ['win32']:
    from twisted.internet.iocpreactor import reactor as iocpreactor
    iocpreactor.install()
elif sys.platform.startswith('linux'):
    from twisted.internet import epollreactor
    epollreactor.install()
else:
    from twisted.internet import default as defaultreactor
    defaultreactor.install()

from twisted.web.server import Site  # noqa
from twisted.web.resource import Resource  # noqa
from twisted.internet import reactor  # noqa


class TestResource(Resource):
    def render_GET(self, request):
        body = ujson.dumps({'test': True}).encode('utf8')
        request.setHeader(b'Content-Type', b'application/json; charset=utf-8')
        return body

    def getChild(self, path, request):
        if path == b'payload':
            return Payload()