@@ -66,6 +66,8 @@ Functions and classes provided:
66
66
# Code to release resource, e.g.:
67
67
release_resource(resource)
68
68
69
+ The function can then be used like this::
70
+
69
71
>>> with managed_resource(timeout=3600) as resource:
70
72
... # Resource is released at the end of this block,
71
73
... # even if code in the block raises an exception
@@ -140,9 +142,9 @@ Functions and classes provided:
140
142
finally:
141
143
print(f'it took {time.monotonic() - now}s to run')
142
144
143
- @timeit()
144
- async def main():
145
- # ... async code ...
145
+ @timeit()
146
+ async def main():
147
+ # ... async code ...
146
148
147
149
When used as a decorator, a new generator instance is implicitly created on
148
150
each function call. This allows the otherwise "one-shot" context managers
@@ -249,15 +251,15 @@ Functions and classes provided:
249
251
:ref: `asynchronous context managers <async-context-managers >`::
250
252
251
253
async def send_http(session=None):
252
- if not session:
253
- # If no http session, create it with aiohttp
254
- cm = aiohttp.ClientSession()
255
- else:
256
- # Caller is responsible for closing the session
257
- cm = nullcontext(session)
254
+ if not session:
255
+ # If no http session, create it with aiohttp
256
+ cm = aiohttp.ClientSession()
257
+ else:
258
+ # Caller is responsible for closing the session
259
+ cm = nullcontext(session)
258
260
259
- async with cm as session:
260
- # Send http requests with session
261
+ async with cm as session:
262
+ # Send http requests with session
261
263
262
264
.. versionadded :: 3.7
263
265
@@ -379,6 +381,8 @@ Functions and classes provided:
379
381
print('Finishing')
380
382
return False
381
383
384
+ The class can then be used like this::
385
+
382
386
>>> @mycontext()
383
387
... def function():
384
388
... print('The bit in the middle')
@@ -449,6 +453,8 @@ Functions and classes provided:
449
453
print('Finishing')
450
454
return False
451
455
456
+ The class can then be used like this::
457
+
452
458
>>> @mycontext()
453
459
... async def function():
454
460
... print('The bit in the middle')
0 commit comments