def install_reactor(explicitReactor=None, verbose=False):
"""
Install Twisted reactor.
:param explicitReactor: If provided, install this reactor. Else, install optimal reactor.
:type explicitReactor: obj
:param verbose: If ``True``, print what happens.
:type verbose: bool
"""
import sys
if explicitReactor:
## install explicitly given reactor
##
from twisted.application.reactors import installReactor
print("Trying to install explicitly specified Twisted reactor '%s'" % explicitReactor)
try:
installReactor(explicitReactor)
except Exception as e:
print("Could not install Twisted reactor %s%s" % (explicitReactor, ' ["%s"]' % e if verbose else ''))
sys.exit(1)
else:
## automatically choose optimal reactor
##
if verbose:
print("Automatically choosing optimal Twisted reactor")
install_optimal_reactor(verbose)
## now the reactor is installed, import it
from twisted.internet import reactor
if verbose:
from twisted.python.reflect import qual
print("Running Twisted reactor %s" % qual(reactor.__class__))
return reactor
评论列表
文章目录