Fix Python SDK timer: use async loop#82
Merged
Merged
Conversation
Co-authored-by: Divkix <Divkix@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Plan
BatchQueueusesthreading.Timerfor interval-based flushes. The timer fires in a daemon thread with no running event loop, so_on_timer_expiredfalls back toasyncio.run(self.flush())— creating and destroying a new event loop on every flush. This breakshttpx.AsyncClientconnection pool reuse and wastes resources.threading.Timerwith a dedicated background event loop thread (_queue_loop+_queue_thread). All flush work (timer-triggered and batch-triggered from sync contexts) is submitted to this single persistent loop viaasyncio.run_coroutine_threadsafe(). The publicflush()method dispatches to the background loop and awaits the result withasyncio.wrap_future().asyncio.Eventinside mocksend_batchfunctions. Because the mock now runs in the background loop, those events were tied to the wrong loop. Updated those tests to usethreading.Eventwith polling loops.What Changed
sdks/python/src/logwell/queue.py_ensure_loop,_run_loop,_stop_loop).threading.Timerwith anasyncio-based timer (_timer_coro) that sleeps and then schedules flushes on the background loop.flush()into a dispatcher that routes to the background loop, and_do_flush()that contains the actual logic._trigger_flush()now always submits work to the background loop instead of callingasyncio.run().sdks/python/tests/unit/test_queue.pytest_concurrent_flush_preventedandtest_entries_added_during_flush_are_preserved: replacedasyncio.Eventwiththreading.Event+ polling loops so the mocksend_batchworks correctly when executed in the background loop.Verification
ruff) and type check (mypy) both pass.Closes #74
opencode session | github run