Skip to content

Commit

Permalink
Add util.getLoop() and use it throughout, fixes a depreciation warnin…
Browse files Browse the repository at this point in the history
…g from asyncio
  • Loading branch information
erdewit committed Jul 26, 2022
1 parent 1483b6a commit c137917
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 13 deletions.
3 changes: 2 additions & 1 deletion examples/qt_ticker_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def onConnectButtonClicked(self, _):
self.add("Stock('TSLA', 'SMART', 'USD')")

def closeEvent(self, ev):
asyncio.get_event_loop().stop()
loop = util.getLoop()
loop.stop()


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion examples/tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self):
self.button.grid()
self.text = tk.Text(self.root)
self.text.grid()
self.loop = asyncio.get_event_loop()
self.loop = util.getLoop()

def onButtonClick(self):
contract = eval(self.entry.get())
Expand Down
6 changes: 3 additions & 3 deletions ib_insync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .contract import Contract
from .decoder import Decoder
from .objects import ConnectionStats
from .util import UNSET_DOUBLE, UNSET_INTEGER, dataclassAsTuple, run
from .util import UNSET_DOUBLE, UNSET_INTEGER, dataclassAsTuple, getLoop, run


class Client:
Expand Down Expand Up @@ -133,7 +133,7 @@ def serverVersion(self) -> int:
return self._serverVersion

def run(self):
loop = asyncio.get_event_loop()
loop = getLoop()
loop.run_forever()

def isConnected(self):
Expand Down Expand Up @@ -263,7 +263,7 @@ def send(self, *fields):
self.sendMsg(msg.getvalue())

def sendMsg(self, msg):
loop = asyncio.get_event_loop()
loop = getLoop()
t = loop.time()
times = self._timeQ
msgs = self._msgQ
Expand Down
4 changes: 3 additions & 1 deletion ib_insync/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from eventkit import Event

from ib_insync.util import getLoop


class Connection(asyncio.Protocol):
"""
Expand Down Expand Up @@ -33,7 +35,7 @@ async def connectAsync(self, host, port):
self.disconnect()
await self.disconnected
self.reset()
loop = asyncio.get_event_loop()
loop = getLoop()
self.transport, _ = await loop.create_connection(
lambda: self, host, port)

Expand Down
3 changes: 2 additions & 1 deletion ib_insync/ib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,8 @@ async def requestFAAsync(self, faDataType: int):


if __name__ == '__main__':
asyncio.get_event_loop().set_debug(True)
loop = util.getLoop()
loop.set_debug(True)
util.logToConsole(logging.DEBUG)
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
Expand Down
11 changes: 8 additions & 3 deletions ib_insync/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def run(*awaitables: Awaitable, timeout: float = None):
asyncio.TimeoutError if the awaitables are not ready within the
timeout period.
"""
loop = asyncio.get_event_loop()
loop = getLoop()
if not awaitables:
if loop.is_running():
return
Expand Down Expand Up @@ -363,7 +363,7 @@ def schedule(
dt = _fillDate(time)
now = datetime.now(dt.tzinfo)
delay = (dt - now).total_seconds()
loop = asyncio.get_event_loop()
loop = getLoop()
return loop.call_later(delay, callback, *args)


Expand Down Expand Up @@ -453,6 +453,11 @@ def patchAsyncio():
nest_asyncio.apply()


def getLoop():
"""Get the asyncio event loop for the current thread."""
return asyncio.get_event_loop_policy().get_event_loop()


def startLoop():
"""Use nested asyncio event loop for Jupyter notebooks."""
patchAsyncio()
Expand Down Expand Up @@ -493,7 +498,7 @@ def qt_step():
global qApp
qApp = (qw.QApplication.instance() # type: ignore
or qw.QApplication(sys.argv)) # type: ignore
loop = asyncio.get_event_loop()
loop = getLoop()
stack: list = []
qt_step()

Expand Down
4 changes: 2 additions & 2 deletions ib_insync/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ib_insync.ticker import Ticker
from ib_insync.util import (
UNSET_DOUBLE, UNSET_INTEGER, dataclassAsDict, dataclassUpdate,
globalErrorEvent, isNan, parseIBDatetime)
getLoop, globalErrorEvent, isNan, parseIBDatetime)

OrderKeyType = Union[int, Tuple[int, int]]

Expand Down Expand Up @@ -215,7 +215,7 @@ def _setTimer(self, delay: float = 0):
if not delay:
delay = self._timeout - diff
if delay > 0:
loop = asyncio.get_event_loop()
loop = getLoop()
self._timeoutHandle = loop.call_later(delay, self._setTimer)
else:
self._logger.debug('Timeout')
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@pytest.fixture(scope='session')
def event_loop():
loop = asyncio.get_event_loop()
loop = ibi.util.getLoop()
yield loop
loop.close()

Expand Down

0 comments on commit c137917

Please sign in to comment.