Skip to content

Commit a1a7a9c

Browse files
committed
fix asyncio event loop
1 parent 1bfd15d commit a1a7a9c

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

docs/source/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Glossary
3434
Releases
3535
---------------------
3636

37+
v1.0.1
38+
=================
39+
- Fix event loop problems on Python before 3.10 due to semaphores (etc.) calling ``get_event_loop`` inside.
40+
41+
3742
v1.0.0
3843
=================
3944
- Initial release

tk_async_execute/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828
SOFTWARE.
2929
"""
30-
VERSION = "1.0.0"
30+
VERSION = "1.0.1"
3131

3232
from .utils import *
3333
from . import widget

tk_async_execute/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from concurrent.futures import Future as TFuture
3232

3333
import asyncio
34+
import sys
3435

3536
from .widget import ExecutingAsyncWindow
3637
from . import doc
@@ -54,6 +55,7 @@ def stop():
5455
loop = GLOBAL.loop
5556
loop.call_soon_threadsafe(loop.stop)
5657
GLOBAL.async_thread.join()
58+
asyncio.set_event_loop(None)
5759
loop.close()
5860
ExecutingAsyncWindow.loop = None
5961
return loop
@@ -72,7 +74,14 @@ def start():
7274
if GLOBAL.async_thread is not None and GLOBAL.async_thread.is_alive():
7375
raise RuntimeError("Event loop already started. Stop it with ``tk_async_execute.stop()`` first")
7476

75-
loop = asyncio.new_event_loop()
77+
# Semaphores, etc on version prior to 3.10, call get_event_loop inside, which will cause
78+
# exceptions if new_event_loop is created and started.
79+
if sys.version_info.minor < 10:
80+
loop = asyncio.get_event_loop()
81+
else:
82+
loop = asyncio.new_event_loop()
83+
asyncio.set_event_loop(loop)
84+
7685
GLOBAL.loop = loop
7786
GLOBAL.async_thread = Thread(target=loop.run_forever)
7887
ExecutingAsyncWindow.loop = loop

0 commit comments

Comments
 (0)