Skip to content

Commit b9b4fd2

Browse files
ddkohlerpre-commit-ci[bot]untzag
authored
alter asyncio loop syntax (#87)
* Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * Update _is_sensor.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * propagate to HasMeasureTrigger * Update _is_sensor.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove MeasureType * remove redundant actions * expand test py versions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * initiate tests again * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * try python 3.13 curious if the freze issue still persists * Update _is_daemon.py * Update _is_daemon.py * Update _is_daemon.py * refactor asyncio loop * Update _fake_sensor.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * cleanup * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update _is_daemon.py cleanup * peek at py3.8 tests * Update .gitignore * Update CHANGELOG.md * test with py3.14 * don't reach in and grab _loop --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Blaise Thompson <blaise@untzag.com>
1 parent 02baf67 commit b9b4fd2

6 files changed

Lines changed: 24 additions & 32 deletions

File tree

.github/workflows/python-pytest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010

1111
strategy:
1212
matrix:
13-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
13+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
1414
os: [ubuntu-latest, windows-latest]
1515
runs-on: ${{ matrix.os }}
1616

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ coverage.xml
5252
# vim
5353
*.sw?
5454

55+
/.vscode

yaqd-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
66
## [Unreleased]
77

88
### Fixed
9+
- removed asyncio syntax that was removed in python 3.14
910
- type hints for IsSensor attributes are appropriate for _n_-dimensional data
1011

1112
## [2023.11.0]

yaqd-core/yaqd_core/_is_daemon.py

100755100644
Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,7 @@ def _traits(cls) -> List[str]:
123123

124124
@classmethod
125125
def main(cls):
126-
"""Run the event loop."""
127-
loop = asyncio.get_event_loop()
128-
if sys.platform.startswith("win"):
129-
signals = ()
130-
else:
131-
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
132-
for s in signals:
133-
loop.add_signal_handler(
134-
s, lambda s=s: asyncio.create_task(cls.shutdown_all(s, loop))
135-
)
136-
126+
"""parse arguments and start the event loop"""
137127
parser = argparse.ArgumentParser()
138128
parser.add_argument(
139129
"--config",
@@ -199,19 +189,26 @@ def main(cls):
199189
with open(config_filepath, "rb") as f:
200190
config_file = tomli.load(f)
201191

202-
loop.create_task(cls._main(config_filepath, config_file, args))
192+
# Run the event loop
203193
try:
204-
loop.run_forever()
194+
asyncio.run(cls._main(config_filepath, config_file, args))
205195
except asyncio.CancelledError:
206196
pass
207-
finally:
208-
loop.close()
209197

210198
@classmethod
211199
async def _main(cls, config_filepath, config_file, args=None):
212-
"""Parse command line arguments, start event loop tasks."""
200+
"""Parse command line arguments, run event loop."""
213201
loop = asyncio.get_running_loop()
214-
cls.__servers = []
202+
if sys.platform.startswith("win"):
203+
signals = ()
204+
else:
205+
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
206+
for s in signals:
207+
loop.add_signal_handler(
208+
s, lambda s=s: asyncio.create_task(cls.shutdown_all(s, loop))
209+
)
210+
211+
cls.__servers = set()
215212
for section in config_file:
216213
if section == "shared-settings":
217214
continue
@@ -225,7 +222,7 @@ async def _main(cls, config_filepath, config_file, args=None):
225222

226223
while cls.__servers:
227224
awaiting = cls.__servers
228-
cls.__servers = []
225+
cls.__servers = set()
229226
await asyncio.wait(awaiting)
230227
await asyncio.sleep(1)
231228
loop.stop()
@@ -252,7 +249,9 @@ def server(daemon):
252249
server(daemon), config.get("host", ""), config.get("port", None)
253250
)
254251
daemon._server = ser
255-
cls.__servers.append(asyncio.create_task(ser.serve_forever()))
252+
task = asyncio.create_task(ser.serve_forever())
253+
cls.__servers.add(task)
254+
task.add_done_callback(cls.__servers.discard)
256255

257256
@classmethod
258257
def _parse_config(cls, config_file, section, args=None):
@@ -297,16 +296,7 @@ async def shutdown_all(cls, sig, loop):
297296
# This is done after cancelling so that shutdown tasks which require the loop
298297
# are not themselves cancelled.
299298
[d.close() for d in cls._daemons]
300-
tasks = [
301-
t
302-
for t in asyncio.all_tasks()
303-
if (
304-
t is not asyncio.current_task()
305-
and "serve_forever" not in t.get_coro().__repr__()
306-
)
307-
]
308-
for task in tasks:
309-
logger.info(task.get_coro())
299+
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
310300
await asyncio.gather(*tasks, return_exceptions=True)
311301
[d._save_state() for d in cls._daemons]
312302
if hasattr(signal, "SIGHUP") and sig == signal.SIGHUP:

yaqd-core/yaqd_core/_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def connection_made(self, transport):
2828
self.transport = transport
2929
self.unpacker = avrorpc.Unpacker(self._avro_protocol)
3030
self._daemon._connection_made(peername)
31-
self.task = asyncio.get_event_loop().create_task(self.process_requests())
31+
self.task = asyncio.get_running_loop().create_task(self.process_requests())
3232

3333
def data_received(self, data):
3434
"""Process an incomming request."""

yaqd-fakes/yaqd_fakes/_fake_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, name, config, config_filepath):
2828
self._channel_generators[name] = random_walk(min_, max_)
2929
else:
3030
raise Exception(f"channel kind {kwargs['kind']} not recognized")
31-
asyncio.get_event_loop().create_task(self._update_measurements())
31+
asyncio.get_running_loop().create_task(self._update_measurements())
3232

3333
async def _update_measurements(self):
3434
while True:

0 commit comments

Comments
 (0)