Debian Codegraph @

doc/source/user/examples.rst

raw
1 ========
2 Examples
3 ========
4
5 -----------------------------------------
6 Creating and using a synchronous executor
7 -----------------------------------------
8
9 .. testcode::
10
11 # NOTE: enable printing timestamp for additional data
12
13 import sys
14 import futurist
15 import eventlet
16
17 def delayed_func():
18 print("started")
19 eventlet.sleep(3)
20 print("done")
21
22 #print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
23 e = futurist.SynchronousExecutor()
24 fut = e.submit(delayed_func)
25 eventlet.sleep(1)
26 print("Hello")
27 eventlet.sleep(1)
28 e.shutdown()
29 #print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
30
31 **Expected output:**
32
33 .. testoutput::
34
35 started
36 done
37 Hello
38
39 ------------------------------------------------
40 Creating and using a green thread-based executor
41 ------------------------------------------------
42
43 .. testcode::
44
45 # NOTE: enable printing timestamp for additional data
46
47 import sys
48 import futurist
49 import eventlet
50
51 def delayed_func():
52 print("started")
53 eventlet.sleep(3)
54 print("done")
55
56 #print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
57 e = futurist.GreenThreadPoolExecutor()
58 fut = e.submit(delayed_func)
59 eventlet.sleep(1)
60 print("Hello")
61 eventlet.sleep(1)
62 e.shutdown()
63 #print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
64
65 **Expected output:**
66
67 .. testoutput::
68
69 started
70 Hello
71 done
72
73
74 ------------------------------------------
75 Creating and using a thread-based executor
76 ------------------------------------------
77
78 .. testcode::
79
80 import time
81
82 import futurist
83
84 def delayed_func():
85 time.sleep(0.1)
86 return "hello"
87
88 e = futurist.ThreadPoolExecutor()
89 fut = e.submit(delayed_func)
90 print(fut.result())
91 e.shutdown()
92
93 **Expected output:**
94
95 .. testoutput::
96
97 hello
98
99 -------------------------------------------
100 Creating and using a process-based executor
101 -------------------------------------------
102
103 ::
104
105 import time
106
107 import futurist
108
109 def delayed_func():
110 time.sleep(0.1)
111 return "hello"
112
113 e = futurist.ProcessPoolExecutor()
114 fut = e.submit(delayed_func)
115 print(fut.result())
116 e.shutdown()
117
118 **Expected output:**
119
120 ::
121
122 hello
123
124 ---------------------------------------
125 Running a set of functions periodically
126 ---------------------------------------
127
128 .. testcode::
129
130 import futurist
131 from futurist import periodics
132
133 import time
134 import threading
135
136
137 @periodics.periodic(1)
138 def every_one(started_at):
139 print("1: %s" % (time.time() - started_at))
140
141
142 @periodics.periodic(2)
143 def every_two(started_at):
144 print("2: %s" % (time.time() - started_at))
145
146
147 @periodics.periodic(4)
148 def every_four(started_at):
149 print("4: %s" % (time.time() - started_at))
150
151
152 @periodics.periodic(6)
153 def every_six(started_at):
154 print("6: %s" % (time.time() - started_at))
155
156
157 started_at = time.time()
158 callables = [
159 # The function to run + any automatically provided positional and
160 # keyword arguments to provide to it everytime it is activated.
161 (every_one, (started_at,), {}),
162 (every_two, (started_at,), {}),
163 (every_four, (started_at,), {}),
164 (every_six, (started_at,), {}),
165 ]
166 w = periodics.PeriodicWorker(callables)
167
168 # In this example we will run the periodic functions using a thread, it
169 # is also possible to just call the w.start() method directly if you do
170 # not mind blocking up the current program.
171 t = threading.Thread(target=w.start)
172 t.daemon = True
173 t.start()
174
175 # Run for 10 seconds and then stop.
176 while (time.time() - started_at) <= 10:
177 time.sleep(0.1)
178 w.stop()
179 w.wait()
180 t.join()
181
182 .. testoutput::
183 :hide:
184
185 ...
186
187 -----------------------------------------------------------
188 Running a set of functions periodically (using an executor)
189 -----------------------------------------------------------
190
191 .. testcode::
192
193 import futurist
194 from futurist import periodics
195
196 import time
197 import threading
198
199
200 @periodics.periodic(1)
201 def every_one(started_at):
202 print("1: %s" % (time.time() - started_at))
203 time.sleep(0.5)
204
205
206 @periodics.periodic(2)
207 def every_two(started_at):
208 print("2: %s" % (time.time() - started_at))
209 time.sleep(1)
210
211
212 @periodics.periodic(4)
213 def every_four(started_at):
214 print("4: %s" % (time.time() - started_at))
215 time.sleep(2)
216
217
218 @periodics.periodic(6)
219 def every_six(started_at):
220 print("6: %s" % (time.time() - started_at))
221 time.sleep(3)
222
223
224 started_at = time.time()
225 callables = [
226 # The function to run + any automatically provided positional and
227 # keyword arguments to provide to it everytime it is activated.
228 (every_one, (started_at,), {}),
229 (every_two, (started_at,), {}),
230 (every_four, (started_at,), {}),
231 (every_six, (started_at,), {}),
232 ]
233
234 # To avoid getting blocked up by slow periodic functions we can also
235 # provide a executor pool to make sure that slow functions only block
236 # up a thread (or green thread), instead of blocking other periodic
237 # functions that need to be scheduled to run.
238 executor_factory = lambda: futurist.ThreadPoolExecutor(max_workers=2)
239 w = periodics.PeriodicWorker(callables, executor_factory=executor_factory)
240
241 # In this example we will run the periodic functions using a thread, it
242 # is also possible to just call the w.start() method directly if you do
243 # not mind blocking up the current program.
244 t = threading.Thread(target=w.start)
245 t.daemon = True
246 t.start()
247
248 # Run for 10 seconds and then stop.
249 while (time.time() - started_at) <= 10:
250 time.sleep(0.1)
251 w.stop()
252 w.wait()
253 t.join()
254
255 .. testoutput::
256 :hide:
257
258 ...
259
260 --------------------------------------------------------------------
261 Stopping periodic function to run again (using NeverAgain exception)
262 --------------------------------------------------------------------
263
264 .. testcode::
265
266 import futurist
267 from futurist import periodics
268
269 import time
270 import threading
271
272
273 @periodics.periodic(1)
274 def run_only_once(started_at):
275 print("1: %s" % (time.time() - started_at))
276 raise periodics.NeverAgain("No need to run again after first run !!")
277
278
279 @periodics.periodic(1)
280 def keep_running(started_at):
281 print("2: %s" % (time.time() - started_at))
282
283
284 started_at = time.time()
285 callables = [
286 # The function to run + any automatically provided positional and
287 # keyword arguments to provide to it everytime it is activated.
288 (run_only_once, (started_at,), {}),
289 (keep_running, (started_at,), {}),
290 ]
291 w = periodics.PeriodicWorker(callables)
292
293 # In this example we will run the periodic functions using a thread, it
294 # is also possible to just call the w.start() method directly if you do
295 # not mind blocking up the current program.
296 t = threading.Thread(target=w.start)
297 t.daemon = True
298 t.start()
299
300 # Run for 10 seconds and then stop.
301 while (time.time() - started_at) <= 10:
302 time.sleep(0.1)
303 w.stop()
304 w.wait()
305 t.join()
306
307 .. testoutput::
308 :hide:
309
310 ...
311
312 -------------------------------------------------------------------
313 Auto stopping the periodic worker when no more periodic work exists
314 -------------------------------------------------------------------
315
316 .. testcode::
317
318 import futurist
319 from futurist import periodics
320
321 import time
322 import threading
323
324
325 @periodics.periodic(1)
326 def run_only_once(started_at):
327 print("1: %s" % (time.time() - started_at))
328 raise periodics.NeverAgain("No need to run again after first run !!")
329
330
331 @periodics.periodic(2)
332 def run_for_some_time(started_at):
333 print("2: %s" % (time.time() - started_at))
334 if (time.time() - started_at) > 5:
335 raise periodics.NeverAgain("No need to run again !!")
336
337
338 started_at = time.time()
339 callables = [
340 # The function to run + any automatically provided positional and
341 # keyword arguments to provide to it everytime it is activated.
342 (run_only_once, (started_at,), {}),
343 (run_for_some_time, (started_at,), {}),
344 ]
345 w = periodics.PeriodicWorker(callables)
346
347 # In this example we will run the periodic functions using a thread, it
348 # is also possible to just call the w.start() method directly if you do
349 # not mind blocking up the current program.
350 t = threading.Thread(target=w.start, kwargs={'auto_stop_when_empty': True})
351 t.daemon = True
352 t.start()
353
354 # Run for 10 seconds and then check to find out that it had
355 # already stooped.
356 while (time.time() - started_at) <= 10:
357 time.sleep(0.1)
358 print(w.pformat())
359 t.join()
360
361 .. testoutput::
362 :hide:
363
364 ...