agronholm / agronholm/apscheduler
Add argument `wait` of `remove_job` and `remove_all_jobs`
- 主要言語
- Python
- スター
- 7.6k
- フォーク
- 783
- 平均マージ
- 4日 8時間
- マージ済み PR(30日)
- 5
説明
### Things to check first
- [X] I have searched the existing issues and didn't find my feature already requested there
### Feature description
`remove_job` or `remove_all_jobs` of `BackgroundScheduler` should block when there is still running jobs, such as `shutdown(wait=True)`.
### Use case
Without blocking:
```python
import time
from apscheduler.schedulers.background import BackgroundScheduler
def count(arr):
time.sleep(0.2)
arr[0] += 1
if __name__ == '__main__':
# Scheduler with 5 workers
gconfig = {
'apscheduler.job_defaults.max_instances': 5
}
scheduler = BackgroundScheduler(gconfig)
# The job is increment the first element every 0.1 seconds
# Each increment requires 0.2 seconds
arr = [0]
scheduler.add_job(
count,
'interval',
seconds=0.1,
args=(arr,)
)
scheduler.start()
# Do the jobs in a second
time.sleep(1.0)
print('Value', arr[0]) # Value 7
# Remove the jobs and reset the value
scheduler.remove_all_jobs()
arr[0] = 0
# However, as it is not blocked, there's still running jobs, the value keeps incrementing
time.sleep(1.0)
print('Value', arr[0]) # Value 2
```
With blocking:
```python
import time
from apscheduler.schedulers.background import BackgroundScheduler
def count(arr):
time.sleep(0.2)
arr[0] += 1
if __name__ == '__main__':
# Scheduler with 5 workers
gconfig = {
'apscheduler.job_defaults.max_instances': 5
}
scheduler = BackgroundScheduler(gconfig)
# The job is increment the first element every 0.1 seconds
# Each increment requires 0.2 seconds
arr = [0]
scheduler.add_job(
count,
'interval',
seconds=0.1,
args=(arr,)
)
scheduler.start()
# Do the jobs in a second
time.sleep(1.0)
print('Value', arr[0]) # Value 7
# Remove the jobs and reset the value
scheduler.remove_all_jobs(wait=True)
arr[0] = 0
# The value is reset after all jobs are removed, it keeps 0
time.sleep(1.0)
print('Value', arr[0]) # Value 0
```
コントリビューションガイド
評価
この issue はまだ評価されていません。