Skip to content

Commit

Permalink
Allow user to specify uvloop as the loop type.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrh committed Nov 8, 2017
1 parent cd9771d commit c2cc4f3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
25 changes: 20 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
:target: http://calver.org/


aiorun
🏃 aiorun
======================

Here's the big idea (how you use it):
Expand All @@ -42,8 +42,8 @@ of your ``asyncio``-based application. The ``run()`` function will
run forever. If you want to shut down when ``main()`` completes, just
call ``loop.stop()`` inside it: that will initiate shutdown.

Why?
----
🤔 Why?
----------------

The ``run()`` function will handle **everything** that normally needs
to be done during the shutdown sequence of the application. All you
Expand Down Expand Up @@ -82,8 +82,23 @@ There's not much else to know for general use. `aiorun` has a few special
tools that you might need in unusual circumstances. These are discussed
next.

Smart shield for shutdown
-------------------------
💨 Do you like `uvloop <https://github.com/magicstack/uvloop>`_?
------------------------------------------------------------------

.. code-block:: python
import asyncio, aiorun
async def main():
<snip>
if __name__ == '__main__':
run(main(), use_uvloop=true)
Note that you have to ``pip install uvloop`` yourself.

🛡️ Smart shield for shutdown
---------------------------------

It's unusual, but sometimes you're going to want a coroutine to not get
interrupted by cancellation *during the shutdown sequence*. You'll look in
Expand Down
12 changes: 11 additions & 1 deletion aiorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,19 @@ def run(coro: Optional[Coroutine] = None, *,
shutdown_handler: Optional[
Callable[[Optional[AbstractEventLoop]], None]] = None,
executor_workers: int = 10,
executor: Optional[Executor] = None) -> None:
executor: Optional[Executor] = None,
use_uvloop: bool = False) -> None:
logger.debug('Entering run()')

assert not (loop and use_uvloop), (
"'loop' and 'use_uvloop' parameters are mutually "
"exclusive. (Just make your own uvloop and pass it in)."
)
if use_uvloop:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = loop or get_event_loop()

if coro:
async def new_coro():
"""During shutdown, run_until_complete() will exit
Expand Down
9 changes: 9 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ async def main():
run(main(), loop=newloop())


def test_uvloop():
"""Basic SIGTERM"""
async def main():
await asyncio.sleep(0)
asyncio.get_event_loop().stop()

run(main(), use_uvloop=True)


def test_no_coroutine():
"""Signal should still work without a main coroutine"""
kill(SIGTERM)
Expand Down

0 comments on commit c2cc4f3

Please sign in to comment.