@@ -30,8 +30,9 @@ class CommitScheduler:
30
30
"""
31
31
Scheduler to upload a local folder to the Hub at regular intervals (e.g. push to hub every 5 minutes).
32
32
33
- The scheduler is started when instantiated and run indefinitely. At the end of your script, a last commit is
34
- triggered. Checkout the [upload guide](https://huggingface.co/docs/huggingface_hub/guides/upload#scheduled-uploads)
33
+ The recommended way to use the scheduler is to use it as a context manager. This ensures that the scheduler is
34
+ properly stopped and the last commit is triggered when the script ends. The scheduler can also be stopped manually
35
+ with the `stop` method. Checkout the [upload guide](https://huggingface.co/docs/huggingface_hub/guides/upload#scheduled-uploads)
35
36
to learn more about how to use it.
36
37
37
38
Args:
@@ -78,6 +79,22 @@ class CommitScheduler:
78
79
>>> with csv_path.open("a") as f:
79
80
... f.write("second line")
80
81
```
82
+
83
+ Example using a context manager:
84
+ ```py
85
+ >>> from pathlib import Path
86
+ >>> from huggingface_hub import CommitScheduler
87
+
88
+ >>> with CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path="watched_folder", every=10) as scheduler:
89
+ ... csv_path = Path("watched_folder/data.csv")
90
+ ... with csv_path.open("a") as f:
91
+ ... f.write("first line")
92
+ ... (...)
93
+ ... with csv_path.open("a") as f:
94
+ ... f.write("second line")
95
+
96
+ # Scheduler is now stopped and last commit have been triggered
97
+ ```
81
98
"""
82
99
83
100
def __init__ (
@@ -144,6 +161,15 @@ def stop(self) -> None:
144
161
"""
145
162
self .__stopped = True
146
163
164
+ def __enter__ (self ) -> "CommitScheduler" :
165
+ return self
166
+
167
+ def __exit__ (self , exc_type , exc_value , traceback ) -> None :
168
+ # Upload last changes before exiting
169
+ self .trigger ().result ()
170
+ self .stop ()
171
+ return
172
+
147
173
def _run_scheduler (self ) -> None :
148
174
"""Dumb thread waiting between each scheduled push to Hub."""
149
175
while True :
0 commit comments