Skip to content

Commit

Permalink
add async to query method
Browse files Browse the repository at this point in the history
  • Loading branch information
danfrankj committed Mar 30, 2018
1 parent d4eb7b6 commit c965c19
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions omniduct/databases/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sqlparse
from decorator import decorator
from jinja2 import StrictUndefined, Template
from concurrent.futures import ThreadPoolExecutor

from . import cursor_formatters
from omniduct.caches.base import cached_method
Expand All @@ -18,6 +19,7 @@
from omniduct.utils.docs import quirk_docs
from omniduct.utils.magics import MagicsProvider, process_line_arguments, process_line_cell_arguments


logging.getLogger('requests').setLevel(logging.WARNING)


Expand Down Expand Up @@ -236,15 +238,21 @@ def query(self, statement, format=None, format_opts={}, **kwargs):
Returns:
The results of the query formatted as nominated.
"""
cursor = self.execute(statement, async=False, template=False, **kwargs)
async = kwargs.get('async', False)
cursor = self.execute(statement, template=False, **kwargs)

def finish(cursor):
if self._cursor_empty(cursor):
return None
# Some DBAPI2 cursor implementations error if attempting to extract
# data from an empty cursor, and if so, we simply return None.
formatter = self._get_formatter(format, cursor, **format_opts)
return formatter.dump()

# Some DBAPI2 cursor implementations error if attempting to extract
# data from an empty cursor, and if so, we simply return None.
if self._cursor_empty(cursor):
return None
if not async:
return finish(cursor)

formatter = self._get_formatter(format, cursor, **format_opts)
return formatter.dump()
return ThreadPoolExecutor(max_workers=1).submit(finish, cursor) # returns a future

def stream(self, statement, format=None, format_opts={}, batch=None, **kwargs):
"""
Expand Down

0 comments on commit c965c19

Please sign in to comment.