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

Move execute_with_timeout to core ax #2717

Closed

Conversation

danielcohenlive
Copy link

Summary:
There are a probably a number of use cases for this, so moving to core Ax.
I tried some other methods that wouldn't reliably hang up, which could be a problem.
Previous outtakes:

import concurrent.futures
def execute_with_timeout(partial_func: Callable[..., T], timeout: int) -> T:
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future = executor.submit(partial_func)
        try:
            result = future.result(timeout=timeout)
            return result
        except concurrent.futures.TimeoutError:
            # Handle the timeout error here
            print("Function timed out")
            return None

only usable in async or with asyncio.run()

    loop = asyncio.get_running_loop()

    # Use ThreadPoolExecutor to run the sync function in a separate thread
    with ThreadPoolExecutor() as executor:
        try:
            # Use functools.partial to pass arguments to the function
            result = await asyncio.wait_for(
                loop.run_in_executor(executor, partial(long_running_operation, "value1", 42)),
                timeout=5
            )
            print("Result:", result)
        except asyncio.TimeoutError:
            print("Operation timed out!")

Differential Revision: D61821503

Summary:
There are a probably a number of use cases for this, so moving to core Ax.
I tried some other methods that wouldn't reliably hang up, which could be a problem.
Previous outtakes:
```
import concurrent.futures
def execute_with_timeout(partial_func: Callable[..., T], timeout: int) -> T:
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future = executor.submit(partial_func)
        try:
            result = future.result(timeout=timeout)
            return result
        except concurrent.futures.TimeoutError:
            # Handle the timeout error here
            print("Function timed out")
            return None
```

only usable in async or with `asyncio.run()`
```
    loop = asyncio.get_running_loop()

    # Use ThreadPoolExecutor to run the sync function in a separate thread
    with ThreadPoolExecutor() as executor:
        try:
            # Use functools.partial to pass arguments to the function
            result = await asyncio.wait_for(
                loop.run_in_executor(executor, partial(long_running_operation, "value1", 42)),
                timeout=5
            )
            print("Result:", result)
        except asyncio.TimeoutError:
            print("Operation timed out!")
```

Differential Revision: D61821503
@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Aug 27, 2024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61821503

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.40%. Comparing base (f1d2f88) to head (f78abbd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2717   +/-   ##
=======================================
  Coverage   95.39%   95.40%           
=======================================
  Files         492      492           
  Lines       47920    47967   +47     
=======================================
+ Hits        45715    45762   +47     
  Misses       2205     2205           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 52864e5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants