Skip to content

Commit

Permalink
fix asyncio event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhozic committed Aug 23, 2023
1 parent 1bfd15d commit a1a7a9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Glossary
Releases
---------------------

v1.0.1
=================
- Fix event loop problems on Python before 3.10 due to semaphores (etc.) calling ``get_event_loop`` inside.


v1.0.0
=================
- Initial release
2 changes: 1 addition & 1 deletion tk_async_execute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
VERSION = "1.0.0"
VERSION = "1.0.1"

from .utils import *
from . import widget
11 changes: 10 additions & 1 deletion tk_async_execute/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from concurrent.futures import Future as TFuture

import asyncio
import sys

from .widget import ExecutingAsyncWindow
from . import doc
Expand All @@ -54,6 +55,7 @@ def stop():
loop = GLOBAL.loop
loop.call_soon_threadsafe(loop.stop)
GLOBAL.async_thread.join()
asyncio.set_event_loop(None)
loop.close()
ExecutingAsyncWindow.loop = None
return loop
Expand All @@ -72,7 +74,14 @@ def start():
if GLOBAL.async_thread is not None and GLOBAL.async_thread.is_alive():
raise RuntimeError("Event loop already started. Stop it with ``tk_async_execute.stop()`` first")

loop = asyncio.new_event_loop()
# Semaphores, etc on version prior to 3.10, call get_event_loop inside, which will cause
# exceptions if new_event_loop is created and started.
if sys.version_info.minor < 10:
loop = asyncio.get_event_loop()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

GLOBAL.loop = loop
GLOBAL.async_thread = Thread(target=loop.run_forever)
ExecutingAsyncWindow.loop = loop
Expand Down

0 comments on commit a1a7a9c

Please sign in to comment.