-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from jek/feature/send-async
Adds Signal.send_async for asyncio Author: @jek
- Loading branch information
Showing
8 changed files
with
127 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 |
---|---|---|
|
@@ -20,3 +20,8 @@ | |
|
||
|
||
__version__ = '1.5dev' | ||
|
||
try: | ||
import blinker._async | ||
except (ImportError, SyntaxError): | ||
pass |
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 @@ | ||
import asyncio | ||
|
||
from blinker.base import Signal | ||
|
||
|
||
try: | ||
schedule = asyncio.create_task | ||
except AttributeError: | ||
schedule = asyncio.ensure_future | ||
|
||
|
||
@asyncio.coroutine | ||
def _wrap_plain_value(value): | ||
"""Pass through a coroutine *value* or wrap a plain value.""" | ||
if asyncio.iscoroutine(value): | ||
value = yield from value | ||
return value | ||
|
||
|
||
def send_async(self, *sender, **kwargs): | ||
return [(receiver, schedule(_wrap_plain_value(value))) | ||
for receiver, value | ||
in self.send(*sender, **kwargs)] | ||
|
||
|
||
send_async.__doc__ = Signal.send_async.__doc__ | ||
Signal.send_async = send_async | ||
|
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 @@ | ||
import asyncio | ||
|
||
import blinker | ||
|
||
|
||
def test_send_async(): | ||
calls = [] | ||
|
||
@asyncio.coroutine | ||
def receiver_a(sender): | ||
calls.append(receiver_a) | ||
return 'value a' | ||
|
||
@asyncio.coroutine | ||
def receiver_b(sender): | ||
calls.append(receiver_b) | ||
return 'value b' | ||
|
||
def receiver_c(sender): | ||
calls.append(receiver_c) | ||
return 'value c' | ||
|
||
sig = blinker.Signal() | ||
sig.connect(receiver_a) | ||
sig.connect(receiver_b) | ||
sig.connect(receiver_c) | ||
|
||
@asyncio.coroutine | ||
def collect(): | ||
return sig.send_async() | ||
|
||
loop = asyncio.get_event_loop() | ||
results = loop.run_until_complete(collect()) | ||
|
||
expected = { | ||
receiver_a: 'value a', | ||
receiver_b: 'value b', | ||
receiver_c: 'value c', | ||
} | ||
|
||
assert set(calls) == set(expected.keys()) | ||
collected_results = {v.result() for r, v in results} | ||
assert collected_results == set(expected.values()) |
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