Debian Codegraph @

docs/examples.rst

raw
1 Examples
2 ========
3
4 Eager to get started? This page gives a good introduction to Schedule.
5 It assumes you already have Schedule installed. If you do not, head over to :doc:`installation`.
6
7 Run a job every x minute
8 ~~~~~~~~~~~~~~~~~~~~~~~~
9
10 .. code-block:: python
11
12 import schedule
13 import time
14
15 def job():
16 print("I'm working...")
17
18 # Run job every 3 second/minute/hour/day/week,
19 # Starting 3 second/minute/hour/day/week from now
20 schedule.every(3).seconds.do(job)
21 schedule.every(3).minutes.do(job)
22 schedule.every(3).hours.do(job)
23 schedule.every(3).days.do(job)
24 schedule.every(3).weeks.do(job)
25
26 # Run job every minute at the 23rd second
27 schedule.every().minute.at(":23").do(job)
28
29 # Run job every hour at the 42nd minute
30 schedule.every().hour.at(":42").do(job)
31
32 # Run jobs every 5th hour, 20 minutes and 30 seconds in.
33 # If current time is 02:00, first execution is at 06:20:30
34 schedule.every(5).hours.at("20:30").do(job)
35
36 # Run job every day at specific HH:MM and next HH:MM:SS
37 schedule.every().day.at("10:30").do(job)
38 schedule.every().day.at("10:30:42").do(job)
39 schedule.every().day.at("12:42", "Europe/Amsterdam").do(job)
40
41 # Run job on a specific day of the week
42 schedule.every().monday.do(job)
43 schedule.every().wednesday.at("13:15").do(job)
44
45 while True:
46 schedule.run_pending()
47 time.sleep(1)
48
49 Use a decorator to schedule a job
50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
52 Use the ``@repeat`` to schedule a function.
53 Pass it an interval using the same syntax as above while omitting the ``.do()``.
54
55 .. code-block:: python
56
57 from schedule import every, repeat, run_pending
58 import time
59
60 @repeat(every(10).minutes)
61 def job():
62 print("I am a scheduled job")
63
64 while True:
65 run_pending()
66 time.sleep(1)
67
68 The ``@repeat`` decorator does not work on non-static class methods.
69
70 Pass arguments to a job
71 ~~~~~~~~~~~~~~~~~~~~~~~
72
73 ``do()`` passes extra arguments to the job function
74
75 .. code-block:: python
76
77 import schedule
78
79 def greet(name):
80 print('Hello', name)
81
82 schedule.every(2).seconds.do(greet, name='Alice')
83 schedule.every(4).seconds.do(greet, name='Bob')
84
85 from schedule import every, repeat
86
87 @repeat(every().second, "World")
88 @repeat(every().day, "Mars")
89 def hello(planet):
90 print("Hello", planet)
91
92
93 Cancel a job
94 ~~~~~~~~~~~~
95 To remove a job from the scheduler, use the ``schedule.cancel_job(job)`` method
96
97 .. code-block:: python
98
99 import schedule
100
101 def some_task():
102 print('Hello world')
103
104 job = schedule.every().day.at('22:30').do(some_task)
105 schedule.cancel_job(job)
106
107
108 Run a job once
109 ~~~~~~~~~~~~~~
110
111 Return ``schedule.CancelJob`` from a job to remove it from the scheduler.
112
113 .. code-block:: python
114
115 import schedule
116 import time
117
118 def job_that_executes_once():
119 # Do some work that only needs to happen once...
120 return schedule.CancelJob
121
122 schedule.every().day.at('22:30').do(job_that_executes_once)
123
124 while True:
125 schedule.run_pending()
126 time.sleep(1)
127
128
129 Get all jobs
130 ~~~~~~~~~~~~
131 To retrieve all jobs from the scheduler, use ``schedule.get_jobs()``
132
133 .. code-block:: python
134
135 import schedule
136
137 def hello():
138 print('Hello world')
139
140 schedule.every().second.do(hello)
141
142 all_jobs = schedule.get_jobs()
143
144
145 Cancel all jobs
146 ~~~~~~~~~~~~~~~
147 To remove all jobs from the scheduler, use ``schedule.clear()``
148
149 .. code-block:: python
150
151 import schedule
152
153 def greet(name):
154 print('Hello {}'.format(name))
155
156 schedule.every().second.do(greet)
157
158 schedule.clear()
159
160
161 Get several jobs, filtered by tags
162 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163
164 You can retrieve a group of jobs from the scheduler, selecting them by a unique identifier.
165
166 .. code-block:: python
167
168 import schedule
169
170 def greet(name):
171 print('Hello {}'.format(name))
172
173 schedule.every().day.do(greet, 'Andrea').tag('daily-tasks', 'friend')
174 schedule.every().hour.do(greet, 'John').tag('hourly-tasks', 'friend')
175 schedule.every().hour.do(greet, 'Monica').tag('hourly-tasks', 'customer')
176 schedule.every().day.do(greet, 'Derek').tag('daily-tasks', 'guest')
177
178 friends = schedule.get_jobs('friend')
179
180 Will return a list of every job tagged as ``friend``.
181
182
183 Cancel several jobs, filtered by tags
184 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185
186 You can cancel the scheduling of a group of jobs selecting them by a unique identifier.
187
188 .. code-block:: python
189
190 import schedule
191
192 def greet(name):
193 print('Hello {}'.format(name))
194
195 schedule.every().day.do(greet, 'Andrea').tag('daily-tasks', 'friend')
196 schedule.every().hour.do(greet, 'John').tag('hourly-tasks', 'friend')
197 schedule.every().hour.do(greet, 'Monica').tag('hourly-tasks', 'customer')
198 schedule.every().day.do(greet, 'Derek').tag('daily-tasks', 'guest')
199
200 schedule.clear('daily-tasks')
201
202 Will prevent every job tagged as ``daily-tasks`` from running again.
203
204
205 Run a job at random intervals
206 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207
208 .. code-block:: python
209
210 def my_job():
211 print('Foo')
212
213 # Run every 5 to 10 seconds.
214 schedule.every(5).to(10).seconds.do(my_job)
215
216 ``every(A).to(B).seconds`` executes the job function every N seconds such that A <= N <= B.
217
218
219 Run a job until a certain time
220 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221
222 .. code-block:: python
223
224 import schedule
225 from datetime import datetime, timedelta, time
226
227 def job():
228 print('Boo')
229
230 # run job until a 18:30 today
231 schedule.every(1).hours.until("18:30").do(job)
232
233 # run job until a 2030-01-01 18:33 today
234 schedule.every(1).hours.until("2030-01-01 18:33").do(job)
235
236 # Schedule a job to run for the next 8 hours
237 schedule.every(1).hours.until(timedelta(hours=8)).do(job)
238
239 # Run my_job until today 11:33:42
240 schedule.every(1).hours.until(time(11, 33, 42)).do(job)
241
242 # run job until a specific datetime
243 schedule.every(1).hours.until(datetime(2020, 5, 17, 11, 36, 20)).do(job)
244
245 The ``until`` method sets the jobs deadline. The job will not run after the deadline.
246
247 Time until the next execution
248 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
249 Use ``schedule.idle_seconds()`` to get the number of seconds until the next job is scheduled to run.
250 The returned value is negative if the next scheduled jobs was scheduled to run in the past.
251 Returns ``None`` if no jobs are scheduled.
252
253 .. code-block:: python
254
255 import schedule
256 import time
257
258 def job():
259 print('Hello')
260
261 schedule.every(5).seconds.do(job)
262
263 while 1:
264 n = schedule.idle_seconds()
265 if n is None:
266 # no more jobs
267 break
268 elif n > 0:
269 # sleep exactly the right amount of time
270 time.sleep(n)
271 schedule.run_pending()
272
273
274 Run all jobs now, regardless of their scheduling
275 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
276 To run all jobs regardless if they are scheduled to run or not, use ``schedule.run_all()``.
277 Jobs are re-scheduled after finishing, just like they would if they were executed using ``run_pending()``.
278
279 .. code-block:: python
280
281 import schedule
282
283 def job_1():
284 print('Foo')
285
286 def job_2():
287 print('Bar')
288
289 schedule.every().monday.at("12:40").do(job_1)
290 schedule.every().tuesday.at("16:40").do(job_2)
291
292 schedule.run_all()
293
294 # Add the delay_seconds argument to run the jobs with a number
295 # of seconds delay in between.
296 schedule.run_all(delay_seconds=10)