@@ -624,6 +624,43 @@ def configure_tornado_logger(self):
624
624
handler .setFormatter (formatter )
625
625
logger .addHandler (handler )
626
626
627
+ def _init_asyncio_patch (self ):
628
+ """set default asyncio policy to be compatible with tornado
629
+
630
+ Tornado 6 (at least) is not compatible with the default
631
+ asyncio implementation on Windows
632
+
633
+ Pick the older SelectorEventLoopPolicy on Windows
634
+ if the known-incompatible default policy is in use.
635
+
636
+ Support for Proactor via a background thread is available in tornado 6.1,
637
+ but it is still preferable to run the Selector in the main thread
638
+ instead of the background.
639
+
640
+ do this as early as possible to make it a low priority and overridable
641
+
642
+ ref: https://github.com/tornadoweb/tornado/issues/2608
643
+
644
+ FIXME: if/when tornado supports the defaults in asyncio without threads,
645
+ remove and bump tornado requirement for py38.
646
+ Most likely, this will mean a new Python version
647
+ where asyncio.ProactorEventLoop supports add_reader and friends.
648
+
649
+ """
650
+ if sys .platform .startswith ("win" ) and sys .version_info >= (3 , 8 ):
651
+ import asyncio
652
+
653
+ try :
654
+ from asyncio import WindowsProactorEventLoopPolicy , WindowsSelectorEventLoopPolicy
655
+ except ImportError :
656
+ pass
657
+ # not affected
658
+ else :
659
+ if type (asyncio .get_event_loop_policy ()) is WindowsProactorEventLoopPolicy :
660
+ # WindowsProactorEventLoopPolicy is not compatible with tornado 6
661
+ # fallback to the pre-3.8 default of Selector
662
+ asyncio .set_event_loop_policy (WindowsSelectorEventLoopPolicy ())
663
+
627
664
def init_pdb (self ):
628
665
"""Replace pdb with IPython's version that is interruptible.
629
666
@@ -643,6 +680,7 @@ def init_pdb(self):
643
680
@catch_config_error
644
681
def initialize (self , argv = None ):
645
682
"""Initialize the application."""
683
+ self ._init_asyncio_patch ()
646
684
super ().initialize (argv )
647
685
if self .subapp is not None :
648
686
return
0 commit comments