-
Notifications
You must be signed in to change notification settings - Fork 88
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 piccolo-orm/improved_run_sync
handling edge cases in run_sync
- Loading branch information
Showing
4 changed files
with
64 additions
and
7 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 |
---|---|---|
@@ -1,5 +1,30 @@ | ||
import asyncio | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
|
||
def run_sync(coroutine): | ||
return asyncio.run(coroutine) | ||
""" | ||
Run the coroutine synchronously - trying to accomodate as many edge cases | ||
as possible. | ||
1. When called within a coroutine. | ||
2. When called from `python -m asyncio`, or iPython with %autoawait | ||
enabled, which means an event loop may already be running in the | ||
current thread. | ||
""" | ||
try: | ||
loop = asyncio.get_event_loop() | ||
except RuntimeError: | ||
return asyncio.run(coroutine) | ||
else: | ||
if loop.is_running(): | ||
new_loop = asyncio.new_event_loop() | ||
|
||
with ThreadPoolExecutor(max_workers=1) as executor: | ||
future = executor.submit( | ||
new_loop.run_until_complete, coroutine | ||
) | ||
return future.result() | ||
else: | ||
return loop.run_until_complete(coroutine) |
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,5 +1,4 @@ | ||
aiosqlite==0.16.0 | ||
asgiref==3.3.0 | ||
asyncpg==0.21.0 | ||
black==19.10b0 | ||
colorama==0.4.* | ||
|
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 | ||
from unittest import TestCase | ||
|
||
from piccolo.utils.sync import run_sync | ||
|
||
|
||
class TestSync(TestCase): | ||
def test_sync_simple(self): | ||
""" | ||
Test calling a simple coroutine. | ||
""" | ||
run_sync(asyncio.sleep(0.1)) | ||
|
||
def test_sync_nested(self): | ||
""" | ||
Test calling a coroutine, which contains a call to `run_sync`. | ||
""" | ||
|
||
async def test(): | ||
run_sync(asyncio.sleep(0.1)) | ||
|
||
run_sync(test()) | ||
|
||
def test_sync_stopped_event_loop(self): | ||
""" | ||
Test calling a coroutine, when the current thread has an event loop, | ||
but it isn't running. | ||
""" | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
|
||
run_sync(asyncio.sleep(0.1)) | ||
|
||
asyncio.set_event_loop(None) |