-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR helps the users stuck in a sync context. They had problems before my changes, had real trouble through my changes and now have an easy and performant logic through `with_async_env` so are first-class citizen again. Changes: - simplify the async env creation to the `with_async_env` method - document the new `with_async_env` method
- Loading branch information
Showing
10 changed files
with
223 additions
and
15 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
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,12 @@ | ||
from edgy import Registry, Instance, monkay | ||
|
||
models = Registry(database="sqlite:///db.sqlite", echo=True) | ||
|
||
|
||
async def main(): | ||
# check if settings are loaded | ||
monkay.evaluate_settings_once(ignore_import_errors=False) | ||
# monkey-patch so you can use edgy shell | ||
monkay.set_instance(Instance(registry=registry)) | ||
async with models: | ||
... |
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,14 @@ | ||
from edgy import Registry, Instance, monkay | ||
|
||
models = Registry(database="sqlite:///db.sqlite", echo=True) | ||
|
||
|
||
# check if settings are loaded | ||
monkay.evaluate_settings_once(ignore_import_errors=False) | ||
# monkey-patch app so you can use edgy shell | ||
monkay.set_instance(Instance(registry=registry)) | ||
|
||
|
||
def main(): | ||
with models.with_async_env(): | ||
edgy.run_sync(User.query.all()) |
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,31 @@ | ||
import asyncio | ||
from contextvars import ContextVar | ||
from edgy import Registry, Instance, monkay, run_sync | ||
|
||
models = Registry(database="sqlite:///db.sqlite", echo=True) | ||
|
||
|
||
# multithreading safe | ||
event_loop = ContextVar("event_loop", default=None) | ||
|
||
|
||
def handle_request(): | ||
loop = event_loop.get() | ||
if loop is None: | ||
# eventloops die by default with the thread | ||
loop = asyncio.new_event_loop() | ||
event_loop.set(loop) | ||
with models.with_loop(loop): | ||
edgy.run_sync(User.query.all()) | ||
|
||
|
||
def get_application(): | ||
app = ... | ||
# check if settings are loaded | ||
monkay.evaluate_settings_once(ignore_import_errors=False) | ||
# monkey-patch app so you can use edgy shell | ||
monkay.set_instance(Instance(registry=registry, app=app)) | ||
return app | ||
|
||
|
||
app = get_application() |
21 changes: 21 additions & 0 deletions
21
docs_src/connections/contextmanager_with_loop_and_cleanup.py
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,21 @@ | ||
import asyncio | ||
from edgy import Registry, Instance, monkay, run_sync | ||
|
||
models = Registry(database="sqlite:///db.sqlite", echo=True) | ||
|
||
# check if settings are loaded | ||
monkay.evaluate_settings_once(ignore_import_errors=False) | ||
# monkey-patch app so you can use edgy shell | ||
monkay.set_instance(Instance(registry=registry)) | ||
|
||
loop = asyncio.new_event_loop() | ||
with models.with_loop(loop): | ||
edgy.run_sync(User.query.all()) | ||
|
||
# uses the same loop | ||
with models.with_loop(loop): | ||
edgy.run_sync(User.query.all()) | ||
|
||
|
||
loop.run_until_complete(loop.shutdown_asyncgens()) | ||
loop.close() |
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,53 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
import edgy | ||
from edgy.testing.client import DatabaseTestClient | ||
from tests.settings import DATABASE_URL | ||
|
||
database = DatabaseTestClient( | ||
DATABASE_URL, | ||
full_isolation=False, | ||
use_existing=False, | ||
drop_database=True, | ||
force_rollback=False, | ||
) | ||
models = edgy.Registry(database=database) | ||
|
||
|
||
class User(edgy.StrictModel): | ||
name: str = edgy.fields.CharField(max_length=100) | ||
|
||
class Meta: | ||
registry = models | ||
|
||
|
||
def test_run_sync_lifecyle(): | ||
with models.with_async_env(): | ||
edgy.run_sync(models.create_all()) | ||
user = edgy.run_sync(User(name="edgy").save()) | ||
assert user | ||
assert edgy.run_sync(User.query.get()) == user | ||
|
||
|
||
def test_run_sync_lifecyle_sub(): | ||
with models.with_async_env(), models.with_async_env(): | ||
edgy.run_sync(models.create_all()) | ||
user = edgy.run_sync(User(name="edgy").save()) | ||
assert user | ||
assert edgy.run_sync(User.query.get()) == user | ||
|
||
|
||
def test_run_sync_lifecyle_with_idle_loop(): | ||
with pytest.raises(RuntimeError): | ||
asyncio.get_running_loop() | ||
loop = asyncio.new_event_loop() | ||
with models.with_async_env(loop=loop): | ||
edgy.run_sync(models.create_all()) | ||
user = edgy.run_sync(User(name="edgy").save()) | ||
assert user | ||
assert edgy.run_sync(User.query.get()) == user | ||
loop.close() | ||
with pytest.raises(RuntimeError): | ||
asyncio.get_running_loop() |