Skip to content

Commit

Permalink
Fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrh committed May 2, 2022
1 parent 597d2df commit d54ba3c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
29 changes: 25 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 +404,42 @@ Here's an example:
from aiorun import run, shutdown_waits_for
async def corofn():
await asyncio.sleep(60)
for i in range(10):
print(i)
await asyncio.sleep(1)
print('done!')
async def main():
try:
await shutdown_waits_for(corofn())
except asyncio.CancelledError
except asyncio.CancelledError:
print('oh noes!')
run(main())
If you hit ``CTRL-C`` *before* 60 seconds has passed, you will see
``oh noes!`` printed immediately, and then after 60 seconds (since start),
If you hit ``CTRL-C`` *before* 10 seconds has passed, you will see
``oh noes!`` printed immediately, and then after 10 seconds (since start),
``done!`` is printed, and thereafter the program exits.

Output:

.. code-block:: shell
$ python testshield.py
0
1
2
3
4
^CStopping the loop
oh noes!
5
6
7
8
9
done!
Behind the scenes, ``all_tasks()`` would have been cancelled by ``CTRL-C``,
*except* ones wrapped in ``shutdown_waits_for()`` calls. In this respect, it
is loosely similar to ``asyncio.shield()``, but with special applicability
Expand Down
16 changes: 16 additions & 0 deletions testshield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import asyncio
from aiorun import run, shutdown_waits_for

async def corofn():
for i in range(10):
print(i)
await asyncio.sleep(1)
print('done!')

async def main():
try:
await shutdown_waits_for(corofn())
except asyncio.CancelledError:
print('oh noes!')

run(main())

0 comments on commit d54ba3c

Please sign in to comment.