fix(async): replace time.sleep with asyncio.sleep in ensure_opg_approval#271
Open
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
Open
Conversation
…_approval Signed-off-by: verseon0980 <klokrc74@gmail.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.
Problem
ensure_opg_approval()and_send_approve_tx()are synchronous functionsthat contain a polling loop calling
time.sleep(1)up to 120 times.Every official example in this repo calls
ensure_opg_approval()directlyinside an
async def main()function withoutawait:Python's asyncio event loop is single-threaded. Calling
time.sleep()inside an async context does not yield to the event loop. It hands control
to the OS sleep directly. This freezes the entire event loop for up to
120 seconds. During that window:
Every developer following the official examples ships broken async code
without knowing it.
Affected file:
src/opengradient/client/opg_token.pyFix
Added two new async functions alongside the existing sync ones:
_send_approve_tx_async()- identical to_send_approve_tx()but usesawait asyncio.sleep(ALLOWANCE_POLL_INTERVAL)instead oftime.sleep(ALLOWANCE_POLL_INTERVAL), so the event loop is free betweeneach poll.
ensure_opg_approval_async()- identical logic toensure_opg_approval()but calls
await _send_approve_tx_async()internally.The original synchronous functions are preserved completely unchanged for
backward compatibility. No existing code breaks.
Changes
import asyncioat the top_send_approve_tx_async()ensure_opg_approval_async()