-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add loop helpers and simplify deploy example
- Loading branch information
Showing
4 changed files
with
79 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import asyncio | ||
import signal | ||
|
||
|
||
def run(*steps): | ||
""" | ||
Helper to run one or more async functions synchronously, with graceful | ||
handling of SIGINT / Ctrl-C. | ||
Returns the return value of the last function. | ||
""" | ||
if not steps: | ||
return | ||
|
||
task = None | ||
run._sigint = False # function attr to allow setting from closure | ||
loop = asyncio.get_event_loop() | ||
|
||
def abort(): | ||
task.cancel() | ||
run._sigint = True | ||
|
||
loop.add_signal_handler(signal.SIGINT, abort) | ||
try: | ||
for step in steps: | ||
task = loop.create_task(step) | ||
loop.run_until_complete(asyncio.wait([task], loop=loop)) | ||
if run._sigint: | ||
raise KeyboardInterrupt() | ||
if task.exception(): | ||
raise task.exception() | ||
return task.result() | ||
finally: | ||
loop.remove_signal_handler(signal.SIGINT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--index-url https://pypi.python.org/simple/ | ||
|
||
dateutil | ||
-e . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import unittest | ||
import juju.loop | ||
|
||
|
||
class TestLoop(unittest.TestCase): | ||
def test_run(self): | ||
async def _test(): | ||
return 'success' | ||
self.assertEqual(juju.loop.run(_test()), 'success') | ||
|
||
def test_run_interrupt(self): | ||
async def _test(): | ||
juju.loop.run._sigint = True | ||
self.assertRaises(KeyboardInterrupt, juju.loop.run, _test()) | ||
|
||
def test_run_exception(self): | ||
async def _test(): | ||
raise ValueError() | ||
self.assertRaises(ValueError, juju.loop.run, _test()) |