Skip to content

Commit b06dd43

Browse files
committed
session - lazily initialize event loop
1 parent 6942dad commit b06dd43

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

planet/http.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ async def alog_response(*args, **kwargs):
282282
self._limiter = _Limiter(rate_limit=RATE_LIMIT, max_workers=MAX_ACTIVE)
283283
self.outcomes: Counter[str] = Counter()
284284

285+
self._loop: asyncio.AbstractEventLoop = None # type: ignore
286+
287+
def _init_loop(self):
288+
if self._loop:
289+
return
290+
285291
# create a dedicated event loop for this httpx session.
286292
def _start_background_loop(loop):
287293
asyncio.set_event_loop(loop)
@@ -294,6 +300,7 @@ def _start_background_loop(loop):
294300
self._loop_thread.start()
295301

296302
def _call_sync(self, f: Awaitable[T]) -> T:
303+
self._init_loop()
297304
return asyncio.run_coroutine_threadsafe(f, self._loop).result()
298305

299306
@classmethod

tests/integration/test_orders_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def test_list_orders_basic(order_descriptions, session):
120120

121121

122122
@respx.mock
123-
def test_list_orders_basic_sync(order_descriptions, session):
123+
def test_list_orders_basic_sync(order_descriptions):
124124
next_page_url = TEST_ORDERS_URL + 'blob/?page_marker=IAmATest'
125125

126126
order1, order2, order3 = order_descriptions
@@ -265,7 +265,7 @@ async def test_create_order_basic(oid,
265265
def test_create_order_basic_sync(oid,
266266
order_description,
267267
order_request,
268-
session):
268+
):
269269
route = respx.post(TEST_ORDERS_URL)
270270
route.return_value = httpx.Response(HTTPStatus.OK, json=order_description)
271271

@@ -337,7 +337,7 @@ async def test_get_order(oid, order_description, session):
337337

338338

339339
@respx.mock
340-
def test_get_order_sync(oid, order_description, session):
340+
def test_get_order_sync(oid, order_description, ):
341341
get_url = f'{TEST_ORDERS_URL}/{oid}'
342342
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
343343
respx.get(get_url).return_value = mock_resp

0 commit comments

Comments
 (0)