Skip to content

Commit

Permalink
Fix asyncio not working in interactive environments
Browse files Browse the repository at this point in the history
  • Loading branch information
davitmamrikishvili committed Nov 30, 2024
1 parent 3e2aa84 commit a05a542
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
12 changes: 3 additions & 9 deletions th2_data_services/data_source/lwdp/commands/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import nest_asyncio
import warnings
from abc import abstractmethod
from typing import List, Optional, Union, Generator, Any, Dict
Expand Down Expand Up @@ -60,6 +61,7 @@
from th2_data_services.data_source.lwdp.page import PageNotFound

Event = dict
nest_asyncio.apply() # This patch allows nested use of asyncio.run() in environments with an existing event loop.

# Available stream formats:
# 1) str
Expand Down Expand Up @@ -667,15 +669,7 @@ def __init__(self, ids: List[str], use_stub=False):

def handle(self, data_source: DataSource) -> List[dict]: # noqa: D102
# return self._sync_handle(data_source)
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None

if loop and loop.is_running():
return loop.create_task(self._async_handle(data_source)).result()
else:
return asyncio.run(self._async_handle(data_source))
return asyncio.run(self._async_handle(data_source))

def _sync_handle(self, data_source: DataSource) -> List[dict]: # noqa: D102
result = []
Expand Down
1 change: 0 additions & 1 deletion th2_data_services/data_source/lwdp/source_api/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from urllib3 import PoolManager, exceptions

from th2_data_services.data_source.lwdp.interfaces.source_api import IHTTPSourceAPI
from th2_data_services.data_source.lwdp.streams import _convert_stream_to_dict_format


# LOG logger = logging.getLogger("th2_data_services")
Expand Down

0 comments on commit a05a542

Please sign in to comment.