-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 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
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,43 @@ | ||
# -*- coding: utf-8 -*- | ||
import asyncio | ||
from .assertions import assert_corofunction | ||
|
||
|
||
def thunk(coro): | ||
""" | ||
A thunk is a subroutine that is created, often automatically, to assist | ||
a call to another subroutine. | ||
Creates a thunk coroutine which returns coroutine function that accepts no | ||
arguments and when invoked it schedules the wrapper coroutine and | ||
returns the final result. | ||
See Wikipedia page for more information about Thunk subroutines: | ||
https://en.wikipedia.org/wiki/Thunk | ||
Arguments: | ||
value (coroutinefunction): wrapped coroutine function to invoke. | ||
Returns: | ||
coroutinefunction | ||
Usage:: | ||
async def task(): | ||
return 'foo' | ||
coro = paco.thunk(task) | ||
await coro() | ||
# => 'foo' | ||
await coro() | ||
# => 'foo' | ||
""" | ||
assert_corofunction(coro=coro) | ||
|
||
@asyncio.coroutine | ||
def wrapper(): | ||
return (yield from coro()) | ||
|
||
return wrapper |
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,28 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
import asyncio | ||
from paco import thunk | ||
from .helpers import run_in_loop | ||
|
||
|
||
@asyncio.coroutine | ||
def task(): | ||
return 'foo' | ||
|
||
|
||
def test_thunk(): | ||
coro = thunk(task) | ||
assert run_in_loop(coro()) == 'foo' | ||
assert run_in_loop(coro()) == 'foo' | ||
assert run_in_loop(coro()) == 'foo' | ||
|
||
|
||
def test_thunk_error(): | ||
with pytest.raises(TypeError): | ||
run_in_loop(None) | ||
|
||
with pytest.raises(TypeError): | ||
run_in_loop(1) | ||
|
||
with pytest.raises(TypeError): | ||
run_in_loop('foo') |