Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with run polling(sleep) #1398

Open
1 task done
smbatyankh opened this issue May 7, 2024 · 4 comments
Open
1 task done

Issue with run polling(sleep) #1398

smbatyankh opened this issue May 7, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@smbatyankh
Copy link

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

Hi OpenAI team,

I'm reaching out to report a possible issue or improvement regarding the polling mechanism in asynchronous functions.

Problem Description

The current implementation of the poll method uses the time.sleep() function, which is blocking and halts the execution of other asynchronous tasks during polling. This behavior affects concurrency, preventing other coroutines from running efficiently.

To Reproduce


Code snippets

import asyncio

async def poll(
    self,
    run_id: str,
    thread_id: str,
    extra_headers: dict | None = None,
    extra_query: dict | None = None,
    extra_body: dict | None = None,
    timeout: float | 'httpx.Timeout' | None = 'NOT_GIVEN',
    poll_interval_ms: int | 'NotGiven' = 'NOT_GIVEN',
) -> 'Run':
    extra_headers = {"X-Stainless-Poll-Helper": "true", **(extra_headers or {})}

    if is_given(poll_interval_ms):
        extra_headers["X-Stainless-Custom-Poll-Interval"] = str(poll_interval_ms)

    terminal_states = {"requires_action", "cancelled", "completed", "failed", "expired"}
    while True:
        response = await self.with_raw_response.retrieve(
            thread_id=thread_id,
            run_id=run_id,
            extra_headers=extra_headers,
            extra_body=extra_body,
            extra_query=extra_query,
            timeout=timeout,
        )

        run = response.parse()
        if run.status in terminal_states:
            return run

        if not is_given(poll_interval_ms):
            from_header = response.headers.get("openai-poll-after-ms")
            if from_header is not None:
                poll_interval_ms = int(from_header)
            else:
                poll_interval_ms = 1000

        await asyncio.sleep(poll_interval_ms / 1000)

OS

macos

Python version

Python v3.11.4

Library version

openai v1.26.0

@smbatyankh smbatyankh added the bug Something isn't working label May 7, 2024
@CharlyJazz
Copy link

Hey just curiosity, do you have a alternative to avoid asyncio.sleep?

@smbatyankh
Copy link
Author

smbatyankh commented May 8, 2024

There are likely a few alternatives available, but they would involve significant changes to the implementation, such as utilizing call_later or queues. If the current implementation already includes a sleep function and there's no specific reason to use a blocking sleep here, it should be simple to switch to a non-blocking sleep.

However, I'm not sure if this was intentional or just an oversight in the implementation.

@rattrayalex
Copy link
Collaborator

Thanks for reporting – I confirm the problem and hope we'll have a fix out soon.

@RobertCraigie
Copy link
Collaborator

This will be fixed in the next release: #1414

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants