def install_reactor(explicit_reactor=None, verbose=False):
"""
Install Twisted reactor.
:param explicit_reactor: If provided, install this reactor. Else, install
the optimal reactor.
:type explicit_reactor: obj
:param verbose: If ``True``, print what happens.
:type verbose: bool
"""
import sys
log = txaio.make_logger()
if explicit_reactor:
# install explicitly given reactor
##
from twisted.application.reactors import installReactor
log.info("Trying to install explicitly specified Twisted reactor '{reactor}'", reactor=explicit_reactor)
try:
installReactor(explicit_reactor)
except:
log.failure("Could not install Twisted reactor {reactor}\n{log_failure.value}",
reactor=explicit_reactor)
sys.exit(1)
else:
# automatically choose optimal reactor
##
log.debug("Automatically choosing optimal Twisted reactor")
install_optimal_reactor(verbose)
# now the reactor is installed, import it
from twisted.internet import reactor
txaio.config.loop = reactor
if verbose:
from twisted.python.reflect import qual
log.debug("Running Twisted reactor {reactor}", reactor=qual(reactor.__class__))
return reactor
评论列表
文章目录