From c137917682bdbaf1a68017858930a4a38477bbee Mon Sep 17 00:00:00 2001 From: Ewald de Wit Date: Tue, 26 Jul 2022 19:33:32 +0200 Subject: [PATCH] Add util.getLoop() and use it throughout, fixes a depreciation warning from asyncio --- examples/qt_ticker_table.py | 3 ++- examples/tk.py | 2 +- ib_insync/client.py | 6 +++--- ib_insync/connection.py | 4 +++- ib_insync/ib.py | 3 ++- ib_insync/util.py | 11 ++++++++--- ib_insync/wrapper.py | 4 ++-- tests/conftest.py | 2 +- 8 files changed, 22 insertions(+), 13 deletions(-) diff --git a/examples/qt_ticker_table.py b/examples/qt_ticker_table.py index 56ce493c2..cd4f6d9bc 100644 --- a/examples/qt_ticker_table.py +++ b/examples/qt_ticker_table.py @@ -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__': diff --git a/examples/tk.py b/examples/tk.py index b58227567..90a3783db 100644 --- a/examples/tk.py +++ b/examples/tk.py @@ -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()) diff --git a/ib_insync/client.py b/ib_insync/client.py index 43751c7d3..d74e64554 100644 --- a/ib_insync/client.py +++ b/ib_insync/client.py @@ -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: @@ -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): @@ -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 diff --git a/ib_insync/connection.py b/ib_insync/connection.py index 43b644636..67f30c895 100644 --- a/ib_insync/connection.py +++ b/ib_insync/connection.py @@ -4,6 +4,8 @@ from eventkit import Event +from ib_insync.util import getLoop + class Connection(asyncio.Protocol): """ @@ -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) diff --git a/ib_insync/ib.py b/ib_insync/ib.py index 157cca647..91c718f6c 100644 --- a/ib_insync/ib.py +++ b/ib_insync/ib.py @@ -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) diff --git a/ib_insync/util.py b/ib_insync/util.py index c7fe84e35..20d553505 100644 --- a/ib_insync/util.py +++ b/ib_insync/util.py @@ -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 @@ -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) @@ -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() @@ -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() diff --git a/ib_insync/wrapper.py b/ib_insync/wrapper.py index 7f3b4d477..019a9cc42 100644 --- a/ib_insync/wrapper.py +++ b/ib_insync/wrapper.py @@ -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]] @@ -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') diff --git a/tests/conftest.py b/tests/conftest.py index 07c2aa893..c8238ad6a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,7 @@ @pytest.fixture(scope='session') def event_loop(): - loop = asyncio.get_event_loop() + loop = ibi.util.getLoop() yield loop loop.close()