Skip to content

Commit

Permalink
Merge pull request #28 from solomiya-hatalyak/background-progress-emi…
Browse files Browse the repository at this point in the history
…tter

[DS-2172] create async progress reporting
  • Loading branch information
solomiya-hatalyak authored Apr 28, 2021
2 parents 3e926df + b6d64a2 commit f953af4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ install:
script:
- coverage run --source=. test.py
- coverage report -m
- pycodestyle --format=pylint --count .
- pycodestyle --format=pylint --max-line-length=120 --count .
2 changes: 1 addition & 1 deletion panoply/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "2.0.3"
__version__ = "2.0.4"
__package_name__ = "panoply-python-sdk"
45 changes: 43 additions & 2 deletions panoply/datasource.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import base64
import traceback
from concurrent.futures import ThreadPoolExecutor
from functools import wraps
from threading import Event

import backoff
from . import events
import requests
import traceback

from . import events
from .errors import PanoplyException


Expand Down Expand Up @@ -143,3 +147,40 @@ def wrapper(*args):
return f(*args)
return wrapper
return _validate_token


def background_progress(message, waiting_interval=10*60):
""" A decorator is used to emit progress while long operation is executed.
For example, for database's data sources such operations might be
declaration of the cursor or counting number of rows.
This decorator should only be used on methods that are waiting for
input/output operations to be completed.
Parameters
----------
message : str
Message that will be emitted while waiting for operation to complete.
waiting_interval : float
Time in seconds to wait between progress emitting.
Defaults to 10 minutes
"""
def _background_progress(func):
@wraps(func)
def wrapper(*args, **kwargs):
self = args[0]
self.log('Creating background progress emitter')
finished = Event()
with ThreadPoolExecutor(max_workers=1) as executor:
func_future = executor.submit(func, *args, **kwargs)
func_future.add_done_callback(lambda future: finished.set())

while not func_future.done():
self.log(message)
self.progress(None, None, message)
finished.wait(timeout=waiting_interval)

return func_future.result()

return wrapper

return _background_progress

0 comments on commit f953af4

Please sign in to comment.