Skip to content

Commit

Permalink
Update function run_async_in_thread
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejridzik committed Mar 11, 2025
1 parent 6db8198 commit 5689aaa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def app_init() -> None:
Database()

global QUERY_THREAD
QUERY_THREAD = threads.start_async_thread(lambda: asyncio.run(search_thread()))
QUERY_THREAD = threads.start_async_thread(search_thread)

global SCHEDULER
SCHEDULER = BackgroundScheduler()
Expand Down
11 changes: 4 additions & 7 deletions api/app/services/threads/threads.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import asyncio
from threading import Thread
from typing import Callable
from typing import Callable, Coroutine, Any


def run_async_in_thread(target_func: Callable[[], None]) -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(target_func())
loop.close()
def run_async_in_thread(target_func: Callable[[], Coroutine[Any, Any, None]]) -> None:
asyncio.run(target_func())


def start_async_thread(target_func: Callable[[], None]) -> Thread:
def start_async_thread(target_func: Callable[[], Coroutine[Any, Any, None]]) -> Thread:
thread = Thread(target=run_async_in_thread, args=(target_func,))
thread.start()
return thread
Expand Down

0 comments on commit 5689aaa

Please sign in to comment.