|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import asyncio |
| 3 | +import contextlib |
4 | 4 | import logging |
5 | 5 | import os |
| 6 | +import threading |
6 | 7 | import typing as t |
7 | | -from contextlib import asynccontextmanager |
8 | 8 | from functools import lru_cache |
9 | 9 | from pathlib import Path |
10 | 10 |
|
|
19 | 19 | from web.server.models import Mode |
20 | 20 |
|
21 | 21 | logger = logging.getLogger(__name__) |
22 | | -get_context_lock = asyncio.Lock() |
| 22 | + |
| 23 | +get_loaded_context_lock = threading.Lock() |
23 | 24 |
|
24 | 25 | MODE_TO_MODULES = { |
25 | 26 | models.Mode.IDE: { |
@@ -82,46 +83,39 @@ def _get_path_to_model_mapping(context: Context) -> dict[Path, Model]: |
82 | 83 | return {model._path: model for model in context._models.values()} |
83 | 84 |
|
84 | 85 |
|
85 | | -async def get_path_to_model_mapping( |
| 86 | +def get_path_to_model_mapping( |
86 | 87 | settings: Settings = Depends(get_settings), |
87 | 88 | ) -> dict[Path, Model]: |
88 | 89 | try: |
89 | | - async with asynccontextmanager(get_loaded_context)(settings) as context: |
| 90 | + with contextlib.contextmanager(get_loaded_context)(settings) as context: |
90 | 91 | return _get_path_to_model_mapping(context) |
91 | 92 | except Exception: |
92 | 93 | logger.exception("Error creating a context") |
93 | 94 | return {} |
94 | 95 |
|
95 | 96 |
|
96 | | -async def get_loaded_context(settings: Settings = Depends(get_settings)) -> t.AsyncGenerator: |
97 | | - loop = asyncio.get_running_loop() |
98 | | - |
| 97 | +def get_loaded_context( |
| 98 | + settings: Settings = Depends(get_settings), |
| 99 | +) -> t.Generator[Context, None]: |
99 | 100 | try: |
100 | | - async with get_context_lock: |
101 | | - yield await loop.run_in_executor( |
102 | | - None, |
103 | | - _get_loaded_context, |
104 | | - settings.project_path, |
105 | | - settings.config, |
106 | | - settings.gateway, |
107 | | - ) |
| 101 | + with get_loaded_context_lock: |
| 102 | + yield _get_loaded_context(settings.project_path, settings.config, settings.gateway) |
108 | 103 | except Exception: |
109 | 104 | raise ApiException( |
110 | 105 | message="Unable to create a loaded context", |
111 | 106 | origin="API -> settings -> get_loaded_context", |
112 | 107 | ) |
113 | 108 |
|
114 | 109 |
|
115 | | -async def get_context(settings: Settings = Depends(get_settings)) -> t.Optional[Context]: |
| 110 | +def get_context(settings: Settings = Depends(get_settings)) -> t.Optional[Context]: |
116 | 111 | try: |
117 | | - async with get_context_lock: |
118 | | - return _get_context(settings.project_path, settings.config, settings.gateway) |
| 112 | + return _get_context(settings.project_path, settings.config, settings.gateway) |
119 | 113 | except Exception: |
120 | 114 | return None |
121 | 115 |
|
122 | 116 |
|
123 | | -async def get_context_or_raise(settings: Settings = Depends(get_settings)) -> Context: |
124 | | - context = await get_context(settings) |
| 117 | +def get_context_or_raise(settings: Settings = Depends(get_settings)) -> Context: |
| 118 | + context = get_context(settings) |
125 | 119 | if not context: |
126 | 120 | raise ApiException( |
127 | 121 | message="Unable to create a context", |
|
0 commit comments