From c0c335f872bcd2394f388147b968271b374eea2a Mon Sep 17 00:00:00 2001 From: "tim.reichard" Date: Mon, 8 Feb 2021 11:55:24 -0600 Subject: [PATCH] Use await in async_db_wrapper --- HISTORY.rst | 8 +++++++- aioradio/file_ingestion.py | 15 +++++++-------- setup.py | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 8bf0cd8..f6849ab 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,10 +3,16 @@ History ======= +v0.10.2 (2021-02-08) +----------------------- + +* Use await in async_db_wrapper instead of using asyncio.get_event_loop.run_until_complete. + + v0.10.1 (2021-02-08) ----------------------- -* Add missing comma in install_requires +* Add missing comma in install_requires. v0.10.0 (2021-02-08) diff --git a/aioradio/file_ingestion.py b/aioradio/file_ingestion.py index cc2a7e7..8c065da 100644 --- a/aioradio/file_ingestion.py +++ b/aioradio/file_ingestion.py @@ -42,14 +42,14 @@ def async_wrapper(func: coroutine) -> Any: """ @functools.wraps(func) - def wrapper(*args, **kwargs) -> Any: + async def wrapper(*args, **kwargs) -> Any: """Decorator wrapper. Returns: Any: any """ - return asyncio.get_event_loop().run_until_complete(func(*args, **kwargs)) + return await func(*args, **kwargs) return wrapper @@ -76,7 +76,7 @@ def parent_wrapper(func: coroutine) -> Any: """ @functools.wraps(func) - def child_wrapper(*args, **kwargs) -> Any: + async def child_wrapper(*args, **kwargs) -> Any: """Decorator child wrapper. All DB established/closed connections and commits or rollbacks take place in the decorator and should never happen within the inner function. @@ -85,7 +85,6 @@ def child_wrapper(*args, **kwargs) -> Any: Any: any """ - async_run = asyncio.get_event_loop().run_until_complete conns = {} rollback = {} @@ -93,11 +92,11 @@ def child_wrapper(*args, **kwargs) -> Any: for item in db_info: if item['db'] in ['pyodbc', 'psycopg2']: - creds = {**json.loads(async_run(get_secret(item['secret'], item['region']))), **{'database': item.get('database', '')}} + creds = {**json.loads(await get_secret(item['secret'], item['region'])), **{'database': item.get('database', '')}} if item['db'] == 'pyodbc': - conns[item['name']] = async_run(establish_pyodbc_connection(**creds, autocommit=False)) + conns[item['name']] = await establish_pyodbc_connection(**creds, autocommit=False) elif item['db'] == 'psycopg2': - conns[item['name']] = async_run(establish_psycopg2_connection(**creds)) + conns[item['name']] = await establish_psycopg2_connection(**creds) rollback[item['name']] = item['rollback'] print(f"ESTABLISHED CONNECTION for {item['name']}") @@ -105,7 +104,7 @@ def child_wrapper(*args, **kwargs) -> Any: error = None try: # run main function - result = async_run(func(*args, **kwargs, conns=conns)) if conns else async_run(func(*args, **kwargs)) + result = await func(*args, **kwargs, conns=conns) if conns else await func(*args, **kwargs) except Exception as err: error = err diff --git a/setup.py b/setup.py index 7365fdb..d9a343f 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = fileobj.read() setup(name='aioradio', - version='0.10.1', + version='0.10.2', description='Generic asynchronous i/o python utilities for AWS services (SQS, S3, DynamoDB, Secrets Manager), Redis, MSSQL (pyodbc), JIRA and more', long_description=long_description, long_description_content_type="text/markdown",