Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ ib_async.egg-info
poetry.lock
*.csv
*.json
tests/__pycache__
4 changes: 1 addition & 3 deletions ib_async/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,10 @@ def patchAsyncio():
nest_asyncio.apply()


@functools.cache
def getLoop():
"""Get asyncio event loop or create one if it doesn't exist."""
try:
# https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_running_loop
loop = asyncio.get_running_loop()
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import asyncio
from threading import Thread

from ib_async import util


def test_get_current_loop():
loop = util.getLoop()
assert loop is not None


def test_create_new_loop():
loop_holder = []

def target():
loop_holder.append(util.getLoop())

thread1 = Thread(target=target)
thread1.start()
thread1.join()
assert loop_holder[0] is not None

thread2 = Thread(target=target)
thread2.start()
thread2.join()
assert loop_holder[1] is not None
assert loop_holder[0] is not loop_holder[1]


def test_loop_can_be_set():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
assert util.getLoop() is loop