Commit 1c060099 authored by Andrew Svetlov's avatar Andrew Svetlov Committed by GitHub

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

* Restore running proactor event loop from non-main thread
Co-Authored-By: default avatarKyle Stanley <aeros167@gmail.com>
parent 998cf1f0
...@@ -11,6 +11,7 @@ import os ...@@ -11,6 +11,7 @@ import os
import socket import socket
import warnings import warnings
import signal import signal
import threading
import collections import collections
from . import base_events from . import base_events
...@@ -627,7 +628,9 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): ...@@ -627,7 +628,9 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
proactor.set_loop(self) proactor.set_loop(self)
self._make_self_pipe() self._make_self_pipe()
self_no = self._csock.fileno() 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, def _make_socket_transport(self, sock, protocol, waiter=None,
extra=None, server=None): extra=None, server=None):
......
...@@ -59,6 +59,25 @@ class ProactorLoopCtrlC(test_utils.TestCase): ...@@ -59,6 +59,25 @@ class ProactorLoopCtrlC(test_utils.TestCase):
thread.join() 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): class ProactorTests(test_utils.TestCase):
def setUp(self): def setUp(self):
......
Restores instantiation of Windows IOCP event loops from the non-main thread.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment