This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author haxor
Recipients asvetlov, epiphyte, haxor, jnwatson, xtreak, yselivanov
Date 2019-07-09.07:24:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562657084.8.0.24515066921.issue34679@roundup.psfhosted.org>
In-reply-to
Content
I believe the scope of this bug may be larger than it originally seemed.

Now that ProactorEventLoop is the default for Python 3.8 (https://bugs.python.org/issue34687), I'm seeing this same problem on Windows when you try to call asyncio.new_event_loop() from within a thread. It breaks with the ProactorEventLoop (snippet #1 below). It works fine with the SelectorEventLoop (snippet #2 below).

Am I wrong to expect to be able to create a unique event loop for each thread from within the thread itself?

I worked around this problem by manually forcing the event loop policy (https://bugs.python.org/issue33792).

=== Snippet #1 (breaks) ===

import asyncio
from threading import Thread

def my_func():
    asyncio.new_event_loop()

t = Thread(target=my_func)
t.start()
t.join()

=== Output from snippet #1 ===

PS G:\> python .\repro.py
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File ".\repro.py", line 6, in my_func
    asyncio.new_event_loop()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 758, in new_event_loop
    return get_event_loop_policy().new_event_loop()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 656, in new_event_loop
    return self._loop_factory()
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 310, in __init__
    super().__init__(proactor)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 630, in __init__
    signal.set_wakeup_fd(self_no)
ValueError: set_wakeup_fd only works in main thread

=== Snippet #2 (works) ===

import asyncio
from threading import Thread

# Work-around from https://bugs.python.org/issue34679
policy = asyncio.get_event_loop_policy()
policy._loop_factory = asyncio.SelectorEventLoop

def my_func():
    asyncio.new_event_loop()

t = Thread(target=my_func)
t.start()
t.join()

=== More details ===

My version of Python:

3.8.0b2 (tags/v3.8.0b2:21dd01d, Jul  4 2019, 16:00:09) [MSC v.1916 64 bit (AMD64)]

My version of Windows (it's a developer VM from https://developer.microsoft.com/en-us/windows/downloads/virtual-machines):

PS G:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      17763  0
History
Date User Action Args
2019-07-09 07:24:44haxorsetrecipients: + haxor, asvetlov, yselivanov, jnwatson, xtreak, epiphyte
2019-07-09 07:24:44haxorsetmessageid: <1562657084.8.0.24515066921.issue34679@roundup.psfhosted.org>
2019-07-09 07:24:44haxorlinkissue34679 messages
2019-07-09 07:24:44haxorcreate