Skip to content

Commit

Permalink
Merge pull request #15 from nrccua/ARCH-535-use-await-within-async_db…
Browse files Browse the repository at this point in the history
…_wrapper

Use await in async_db_wrapper
  • Loading branch information
nrccua-timr authored Feb 8, 2021
2 parents f62d2d8 + c0c335f commit 4e0548c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
8 changes: 7 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions aioradio/file_ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -85,27 +85,26 @@ def child_wrapper(*args, **kwargs) -> Any:
Any: any
"""

async_run = asyncio.get_event_loop().run_until_complete
conns = {}
rollback = {}

# create connections
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']}")

result = None
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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4e0548c

Please sign in to comment.