From 4cb1a11586d6eac64c36ea34e2e1d9040e3800a9 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Wed, 27 May 2020 08:55:27 +0200 Subject: [PATCH] Switch the update loop from threads to asyncio --- app/__init__.py | 5 +++-- app/fetch.py | 13 +------------ 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 66a79c2..89c7deb 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,11 +2,12 @@ # SPDX-License-Identifier: MIT import os +import asyncio from fastapi import FastAPI from .web import webapp -from .fetch import start_update_thread +from .fetch import update_loop app = FastAPI(openapi_url=None) @@ -17,4 +18,4 @@ app.mount("/", webapp) @app.on_event("startup") async def startup_event() -> None: if not os.environ.get("NO_UPDATE_THREAD"): - start_update_thread() + asyncio.create_task(update_loop()) diff --git a/app/fetch.py b/app/fetch.py index 771ad41..6a72bfb 100644 --- a/app/fetch.py +++ b/app/fetch.py @@ -6,7 +6,6 @@ import io import tarfile import json import asyncio -import threading import traceback from typing import Any, Dict, Tuple, List, Set @@ -317,7 +316,7 @@ async def update_arch_mapping() -> None: state.arch_mapping = ArchMapping(json.loads(data)) -async def update_thread_async() -> None: +async def update_loop() -> None: while True: try: print("check for update") @@ -342,13 +341,3 @@ async def update_thread_async() -> None: traceback.print_exc() print("Sleeping for %d" % UPDATE_INTERVAL) await asyncio.sleep(UPDATE_INTERVAL) - - -def update_thread() -> None: - asyncio.run(update_thread_async()) - - -def start_update_thread() -> None: - thread = threading.Thread(target=update_thread) - thread.daemon = True - thread.start()