Skip to content

Commit 8e438f2

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: Extract out platform specific code like threading
PiperOrigin-RevId: 769655650
1 parent fa110c2 commit 8e438f2

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/google/adk/platform/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

src/google/adk/platform/thread.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
import threading
18+
from typing import Callable
19+
20+
internal_thread = None
21+
try:
22+
from .internal import thread as internal_thread
23+
except ImportError:
24+
internal_thread = None
25+
26+
27+
def create_thread(target: Callable[..., None], *args, **kwargs):
28+
"""Creates a thread."""
29+
if internal_thread:
30+
return internal_thread.create_thread(target, *args, **kwargs)
31+
return threading.Thread(target=target, args=args, kwargs=kwargs)

src/google/adk/runners.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from .events.event import Event
3939
from .memory.base_memory_service import BaseMemoryService
4040
from .memory.in_memory_memory_service import InMemoryMemoryService
41+
from .platform.thread import create_thread
4142
from .sessions.base_session_service import BaseSessionService
4243
from .sessions.in_memory_session_service import InMemorySessionService
4344
from .sessions.session import Session
@@ -139,7 +140,7 @@ def _asyncio_thread_main():
139140
finally:
140141
event_queue.put(None)
141142

142-
thread = threading.Thread(target=_asyncio_thread_main)
143+
thread = create_thread(target=_asyncio_thread_main)
143144
thread.start()
144145

145146
# consumes and re-yield the events from background thread.

0 commit comments

Comments
 (0)