@@ -115,7 +115,7 @@ your function also acts as a checkpoint. If you don't, then it
115
115
isn't. So there's nothing stopping someone from writing a function
116
116
like:
117
117
118
- .. code :: python
118
+ .. code-block :: python
119
119
120
120
# technically legal, but bad style:
121
121
async def why_is_this_async ():
@@ -140,7 +140,7 @@ to help you keep track of checkpoints. Pretty sneaky, eh?)
140
140
141
141
A slightly trickier case is a function like:
142
142
143
- .. code :: python
143
+ .. code-block :: python
144
144
145
145
async def sleep_or_not (should_sleep ):
146
146
if should_sleep:
@@ -163,7 +163,7 @@ to make things extra predictable. It's up to you how picky you want to
163
163
be in your code. To give you a more realistic example of what this
164
164
kind of issue looks like in real life, consider this function:
165
165
166
- .. code :: python
166
+ .. code-block :: python
167
167
168
168
async def recv_exactly (sock , nbytes ):
169
169
data = bytearray ()
@@ -257,7 +257,7 @@ A simple timeout example
257
257
258
258
In the simplest case, you can apply a timeout to a block of code:
259
259
260
- .. code :: python
260
+ .. code-block :: python
261
261
262
262
with trio.move_on_after(30 ):
263
263
result = await do_http_get(" https://..." )
@@ -360,7 +360,7 @@ that raised it, at which point it will stop automatically.
360
360
361
361
Here's an example:
362
362
363
- .. code :: python
363
+ .. code-block :: python
364
364
365
365
print (" starting..." )
366
366
with trio.move_on_after(5 ):
@@ -391,7 +391,7 @@ easier, :func:`move_on_after`\´s ``__enter__`` function returns an
391
391
object representing this cancel scope, which we can use to check
392
392
whether this scope caught a :exc: `Cancelled ` exception:
393
393
394
- .. code :: python
394
+ .. code-block :: python
395
395
396
396
with trio.move_on_after(5 ) as cancel_scope:
397
397
await trio.sleep(10 )
@@ -411,7 +411,7 @@ resource clean-up. For example, imagine that we have a function that
411
411
connects to a remote server and sends some messages, and then cleans
412
412
up on the way out:
413
413
414
- .. code :: python
414
+ .. code-block :: python
415
415
416
416
with trio.move_on_after(TIMEOUT ):
417
417
conn = make_connection()
@@ -440,7 +440,7 @@ is no problem (or at least – it's not Trio's problem). To do this,
440
440
create a new scope, and set its :attr: `~CancelScope.shield `
441
441
attribute to :data: `True `:
442
442
443
- .. code :: python
443
+ .. code-block :: python
444
444
445
445
with trio.move_on_after(TIMEOUT ):
446
446
conn = make_connection()
@@ -565,7 +565,7 @@ Cheat sheet:
565
565
* If you want to impose a timeout on a function, but you don't care
566
566
whether it timed out or not:
567
567
568
- .. code :: python
568
+ .. code-block :: python
569
569
570
570
with trio.move_on_after(TIMEOUT ):
571
571
await do_whatever()
@@ -574,7 +574,7 @@ Cheat sheet:
574
574
* If you want to impose a timeout on a function, and then do some
575
575
recovery if it timed out:
576
576
577
- .. code :: python
577
+ .. code-block :: python
578
578
579
579
with trio.move_on_after(TIMEOUT ) as cancel_scope:
580
580
await do_whatever()
@@ -586,7 +586,7 @@ Cheat sheet:
586
586
out then just give up and raise an error for your caller to deal
587
587
with:
588
588
589
- .. code :: python
589
+ .. code-block :: python
590
590
591
591
with trio.fail_after(TIMEOUT ):
592
592
await do_whatever()
@@ -622,15 +622,15 @@ you feel like it. Trio is a bit different: you can't start a child
622
622
task unless you're prepared to be a responsible parent. The way you
623
623
demonstrate your responsibility is by creating a nursery:
624
624
625
- .. code :: python
625
+ .. code-block :: python
626
626
627
627
async with trio.open_nursery() as nursery:
628
628
...
629
629
630
630
And once you have a reference to a nursery object, you can start
631
631
children in that nursery:
632
632
633
- .. code :: python
633
+ .. code-block :: python
634
634
635
635
async def child ():
636
636
...
@@ -676,7 +676,7 @@ finished.
676
676
677
677
A return statement will not cancel the nursery if it still has tasks running:
678
678
679
- .. code :: python
679
+ .. code-block :: python
680
680
681
681
async def main ():
682
682
async with trio.open_nursery() as nursery:
@@ -694,7 +694,7 @@ In Trio, child tasks inherit the parent nursery's cancel scopes. So in
694
694
this example, both the child tasks will be cancelled when the timeout
695
695
expires:
696
696
697
- .. code :: python
697
+ .. code-block :: python
698
698
699
699
with trio.move_on_after(TIMEOUT ):
700
700
async with trio.open_nursery() as nursery:
@@ -706,7 +706,7 @@ Note that what matters here is the scopes that were active when
706
706
``start_soon `` is called. So for example, the timeout block below does
707
707
nothing at all:
708
708
709
- .. code :: python
709
+ .. code-block :: python
710
710
711
711
async with trio.open_nursery() as nursery:
712
712
with trio.move_on_after(TIMEOUT ): # don't do this!
@@ -727,7 +727,7 @@ Normally, in Python, only one thing happens at a time, which means
727
727
that only one thing can go wrong at a time. Trio has no such
728
728
limitation. Consider code like:
729
729
730
- .. code :: python
730
+ .. code-block :: python
731
731
732
732
async def broken1 ():
733
733
d = {}
@@ -751,7 +751,7 @@ encapsulate multiple exceptions being raised at once.
751
751
To catch individual exceptions encapsulated in an exception group, the ``except* ``
752
752
clause was introduced in Python 3.11 (:pep: `654 `). Here's how it works:
753
753
754
- .. code :: python
754
+ .. code-block :: python
755
755
756
756
try :
757
757
async with trio.open_nursery() as nursery:
@@ -772,7 +772,7 @@ But what if you can't use Python 3.11, and therefore ``except*``, just yet?
772
772
The same exceptiongroup _ library which backports `ExceptionGroup ` also lets
773
773
you approximate this behavior with exception handler callbacks:
774
774
775
- .. code :: python
775
+ .. code-block :: python
776
776
777
777
from exceptiongroup import catch
778
778
@@ -796,7 +796,7 @@ The semantics for the handler functions are equal to ``except*`` blocks, except
796
796
setting local variables. If you need to set local variables, you need to declare them
797
797
inside the handler function(s) with the ``nonlocal `` keyword:
798
798
799
- .. code :: python
799
+ .. code-block :: python
800
800
801
801
def handle_keyerrors (excgroup ):
802
802
nonlocal myflag
@@ -907,7 +907,7 @@ The solution here is simple once you see it: there's no requirement
907
907
that a nursery object stay in the task that created it! We can write
908
908
code like this:
909
909
910
- .. code :: python
910
+ .. code-block :: python
911
911
912
912
async def new_connection_listener (handler , nursery ):
913
913
while True :
@@ -923,7 +923,7 @@ Notice that ``server`` opens a nursery and passes it to
923
923
able to start new tasks as "siblings" of itself. Of course, in this
924
924
case, we could just as well have written:
925
925
926
- .. code :: python
926
+ .. code-block :: python
927
927
928
928
async def server (handler ):
929
929
async with trio.open_nursery() as nursery:
@@ -939,7 +939,7 @@ nursery, **not** from the task that calls ``start_soon``. So in this
939
939
example, the timeout does *not * apply to ``child `` (or to anything
940
940
else):
941
941
942
- .. code :: python
942
+ .. code-block :: python
943
943
944
944
async def do_spawn (nursery ):
945
945
with trio.move_on_after(TIMEOUT ): # don't do this, it has no effect
@@ -969,7 +969,7 @@ For example, here's a function that takes a list of functions, runs
969
969
them all concurrently, and returns the result from the one that
970
970
finishes first:
971
971
972
- .. code :: python
972
+ .. code-block :: python
973
973
974
974
async def race (* async_fns ):
975
975
if not async_fns:
@@ -1169,7 +1169,7 @@ an unfair lock, this would result in the same task holding the lock
1169
1169
forever and the other task being starved out. But if you run this,
1170
1170
you'll see that the two tasks politely take turns:
1171
1171
1172
- .. code :: python
1172
+ .. code-block :: python
1173
1173
1174
1174
# fairness-demo.py
1175
1175
@@ -1366,7 +1366,7 @@ objects using ``async with``. Another option is to pass clones into
1366
1366
all-but-one of the child tasks, and then pass the original object into
1367
1367
the last task, like:
1368
1368
1369
- .. code :: python
1369
+ .. code-block :: python
1370
1370
1371
1371
# Also works, but is more finicky:
1372
1372
send_channel, receive_channel = trio.open_memory_channel(0 )
@@ -1380,7 +1380,7 @@ the producers/consumers.
1380
1380
1381
1381
Just make sure that you don't write:
1382
1382
1383
- .. code :: python
1383
+ .. code-block :: python
1384
1384
1385
1385
# Broken, will cause program to hang:
1386
1386
send_channel, receive_channel = trio.open_memory_channel(0 )
@@ -1586,7 +1586,7 @@ over them. :pep:`525` has many more details if you want them.
1586
1586
For example, the following is a roundabout way to print
1587
1587
the numbers 0 through 9 with a 1-second delay before each one:
1588
1588
1589
- .. code :: python
1589
+ .. code-block :: python
1590
1590
1591
1591
async def range_slowly (* args ):
1592
1592
""" Like range(), but adds a 1-second sleep before each value."""
@@ -1636,7 +1636,7 @@ soon as you're done using it, then you'll need to wrap your use of the
1636
1636
generator in something like `async_generator.aclosing()
1637
1637
<https://async-generator.readthedocs.io/en/latest/reference.html#context-managers> `__:
1638
1638
1639
- .. code :: python
1639
+ .. code-block :: python
1640
1640
1641
1641
# Instead of this:
1642
1642
async for value in my_generator():
@@ -1696,7 +1696,7 @@ Cancel scopes and nurseries
1696
1696
1697
1697
That is, this is OK:
1698
1698
1699
- .. code :: python
1699
+ .. code-block :: python
1700
1700
1701
1701
async def some_agen ():
1702
1702
with trio.move_on_after(1 ):
@@ -1710,7 +1710,7 @@ That is, this is OK:
1710
1710
1711
1711
But this is not:
1712
1712
1713
- .. code :: python
1713
+ .. code-block :: python
1714
1714
1715
1715
async def some_agen ():
1716
1716
with trio.move_on_after(1 ):
@@ -1857,7 +1857,7 @@ example of defining a custom policy that respects the global thread
1857
1857
limit, while making sure that no individual user can use more than 3
1858
1858
threads at a time:
1859
1859
1860
- .. code :: python
1860
+ .. code-block :: python
1861
1861
1862
1862
class CombinedLimiter :
1863
1863
def __init__ (self , first , second ):
0 commit comments