Skip to content

Commit

Permalink
Documentation on propagate exceptions feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dchevell committed Apr 8, 2019
1 parent 1ee1357 commit 8e239c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
Binary file added .DS_Store
Binary file not shown.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,25 @@ Future objects can have callbacks attached by using `Future.add_done_callback`.
lets you specify default callbacks that will be applied to all new futures created by the executor:

```python
def some_callback(future):
# do something with future

executor.add_default_done_callback(some_callback)
def some_callback(future):
# do something with future

# Callback will be added to the below task automatically
executor.submit(pow, 323, 1235)
executor.add_default_done_callback(some_callback)

# Callback will be added to the below task automatically
executor.submit(pow, 323, 1235)
```


Propagate Exceptions
--------------------

Normally any exceptions thrown by background threads or processes will be swallowed unless explicitly
checked for. To instead surface all exceptions thrown by background tasks, Flask-Executor can add
a special default callback that raises any exceptions thrown by tasks submitted to the executor::

```python
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
```


Expand Down
10 changes: 10 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ callbacks that will be applied to all new futures created by the executor::
executor.submit(pow, 323, 1235)


Propagate Exceptions
--------------------

Normally any exceptions thrown by background threads or processes will be swallowed unless explicitly
checked for. To instead surface all exceptions thrown by background tasks, Flask-Executor can add
a special default callback that raises any exceptions thrown by tasks submitted to the executor::

app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True


Indices and tables
==================

Expand Down
13 changes: 7 additions & 6 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def request_context_test_value(_=None):
def g_context_test_value(_=None):
return g.test_value

def fail():
print(hello)


def test_init(app):
executor = Executor(app)
Expand Down Expand Up @@ -241,13 +244,11 @@ def callback(future):
concurrent.futures.wait([future])
assert hasattr(future, 'test')

def test_propagate_exception_callback(default_app):
default_app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
executor = Executor(default_app)
def fail():
print(hello)
def test_propagate_exception_callback(app):
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
executor = Executor(app)
with pytest.raises(NameError):
with default_app.test_request_context('/'):
with app.test_request_context('/'):
future = executor.submit(fail)
concurrent.futures.wait([future])
assert propagate_exceptions_callback in future._done_callbacks
Expand Down

0 comments on commit 8e239c6

Please sign in to comment.