[Python-checkins] bpo-34679: Restore instantiation Windows IOCP event loop from non-main thread (GH-15492)

Miss Islington (bot) webhook-mailer at python.org
Mon Aug 26 06:14:59 EDT 2019


https://github.com/python/cpython/commit/69d22b8fee442c12829a1032a72489c8133de271
commit: 69d22b8fee442c12829a1032a72489c8133de271
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-08-26T03:14:54-07:00
summary:

bpo-34679: Restore instantiation Windows IOCP event loop from non-main thread (GH-15492)


* Restore running proactor event loop from non-main thread

Co-Authored-By: Kyle Stanley <aeros167 at gmail.com>
(cherry picked from commit 1c0600998681295735a18690fae184b0c9a4ca51)

Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-08-25-18-07-48.bpo-34679.HECzL7.rst
M Lib/asyncio/proactor_events.py
M Lib/test/test_asyncio/test_windows_events.py

diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
index 9b8ae064a892..229f56e6bb9e 100644
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -11,6 +11,7 @@
 import socket
 import warnings
 import signal
+import threading
 import collections
 
 from . import base_events
@@ -627,7 +628,9 @@ def __init__(self, proactor):
         proactor.set_loop(self)
         self._make_self_pipe()
         self_no = self._csock.fileno()
-        signal.set_wakeup_fd(self_no)
+        if threading.current_thread() is threading.main_thread():
+            # wakeup fd can only be installed to a file descriptor from the main thread
+            signal.set_wakeup_fd(self_no)
 
     def _make_socket_transport(self, sock, protocol, waiter=None,
                                extra=None, server=None):
diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py
index 64543268b1ef..d0ba19391fa0 100644
--- a/Lib/test/test_asyncio/test_windows_events.py
+++ b/Lib/test/test_asyncio/test_windows_events.py
@@ -59,6 +59,25 @@ def SIGINT_after_delay():
         thread.join()
 
 
+class ProactorMultithreading(test_utils.TestCase):
+    def test_run_from_nonmain_thread(self):
+        finished = False
+
+        async def coro():
+            await asyncio.sleep(0)
+
+        def func():
+            nonlocal finished
+            loop = asyncio.new_event_loop()
+            loop.run_until_complete(coro())
+            finished = True
+
+        thread = threading.Thread(target=func)
+        thread.start()
+        thread.join()
+        self.assertTrue(finished)
+
+
 class ProactorTests(test_utils.TestCase):
 
     def setUp(self):
diff --git a/Misc/NEWS.d/next/Library/2019-08-25-18-07-48.bpo-34679.HECzL7.rst b/Misc/NEWS.d/next/Library/2019-08-25-18-07-48.bpo-34679.HECzL7.rst
new file mode 100644
index 000000000000..785b06b6482c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-08-25-18-07-48.bpo-34679.HECzL7.rst
@@ -0,0 +1 @@
+Restores instantiation of Windows IOCP event loops from the non-main thread.



More information about the Python-checkins mailing list