agronholm / agronholm/apscheduler
Allow jobs to time out
- Ngôn ngữ chính
- Python
- Star
- 7.6k
- Fork
- 783
- Merge trung bình
- 4 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 5
Mô tả
### Things to check first
- [X] I have searched the existing issues and didn't find my feature already requested there
### Feature description
This is mentioned in the Roadmap (#465). I am just opening this issue as a place to discuss this feature and how to realize it as I am currently trying to program something like this.
In the Roadmap, the point I am referring to is 'Timeouts for jobs'. I will precise now how I would try to realize this. If you have any other wishes/ideas/suggestions on how this feature should be implemented: That's exactly the reason I created this issue (-:
I would add a timeout parameter to the Job structure and would handle the timeout in the different job executors. For example, here is an example of how this could work with the `AsyncJobExecutor`:
```python
class AsyncJobExecutor(JobExecutor):
"""
Executes functions directly on the event loop thread.
If the function returns a coroutine object (or another kind of awaitable), that is
awaited on and its return value is used as the job's return value.
"""
async def run_job(self, func: Callable[..., Any], job: Job) -> Any:
# Convert timeout to seconds if it's a timedelta
timeout_seconds = job.timeout.total_seconds() if isinstance(job.timeout, timedelta) else job.timeout
async def wrapper():
retval = func(*job.args, **job.kwargs)
if isawaitable(retval):
retval = await retval
return retval
try:
async with anyio.fail_after(timeout_seconds):
return await wrapper()
except TimeoutError:
raise JobTimedOutError from None
```
The `JobOutcome` should receive a new state called `timeout` which should be set if the `JobTimedOutError` (or we could also use the `TimeoutError` directly which anyio uses) is raised. The scheduler needs to catch that and handle it appropriately.
### Use case
As this is in the roadmap, I think the use case is clear.
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.