Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion examples/amazon_s3_embedding/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ def _main() -> None:
pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL"))

amazon_s3_text_embedding_flow.setup()
with cocoindex.FlowLiveUpdater(amazon_s3_text_embedding_flow):
with cocoindex.FlowLiveUpdater(amazon_s3_text_embedding_flow) as updater:
updater.abort()
updater.wait()

while True:
updates = updater.next_status_updates()
print(f"Updates: {updates}")
if not updates.active_sources:
break

# Run queries in a loop to demonstrate the query capabilities.
while True:
query = input("Enter search query (or Enter to quit): ")
Expand Down
7 changes: 2 additions & 5 deletions python/cocoindex/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,10 +879,7 @@ def update_all_flows(
"""
Update all flows.
"""
return cast(
dict[str, _engine.IndexUpdateInfo],
execution_context.run(update_all_flows_async(options)),
)
return execution_context.run(update_all_flows_async(options))


async def update_all_flows_async(
Expand Down Expand Up @@ -1037,7 +1034,7 @@ def eval(self, *args: Any, **kwargs: Any) -> T:
"""
Evaluate the transform flow.
"""
return cast(T, execution_context.run(self.eval_async(*args, **kwargs)))
return execution_context.run(self.eval_async(*args, **kwargs))

async def eval_async(self, *args: Any, **kwargs: Any) -> T:
"""
Expand Down
7 changes: 5 additions & 2 deletions python/cocoindex/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import threading
import asyncio
from typing import Any, Coroutine
from typing import Any, Coroutine, TypeVar


T = TypeVar("T")


class _ExecutionContext:
Expand All @@ -26,7 +29,7 @@ def event_loop(self) -> asyncio.AbstractEventLoop:
).start()
return self._event_loop

def run(self, coro: Coroutine[Any, Any, Any]) -> Any:
def run(self, coro: Coroutine[Any, Any, T]) -> T:
"""Run a coroutine in the event loop, blocking until it finishes. Return its result."""
return asyncio.run_coroutine_threadsafe(coro, self.event_loop).result()

Expand Down
Loading