@@ -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
@@ -396,6 +398,8 @@ Functions and classes provided:
396
398
print('Finishing')
397
399
return False
398
400
401
+ The class can then be used like this::
402
+
399
403
>>> @mycontext()
400
404
... def function():
401
405
... print('The bit in the middle')
@@ -466,6 +470,8 @@ Functions and classes provided:
466
470
print('Finishing')
467
471
return False
468
472
473
+ The class can then be used like this::
474
+
469
475
>>> @mycontext()
470
476
... async def function():
471
477
... print('The bit in the middle')
0 commit comments