Skip to content

Commit

Permalink
working on deferred callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbo committed Sep 10, 2017
1 parent aa4f741 commit 74ff207
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 85 deletions.
4 changes: 4 additions & 0 deletions _pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest

from rasa_nlu import data_router
from rasa_nlu.components import ComponentBuilder
from rasa_nlu.config import RasaNLUConfig

Expand All @@ -16,6 +17,9 @@

CONFIG_DEFAULTS_PATH = "sample_configs/config_defaults.json"

# see `rasa_nlu.data_router` for details. avoids deadlock in `deferred_from_future` function during tests
data_router.DEFERRED_RUN_IN_REACTOR_THREAD = False


@pytest.fixture(scope="session")
def component_builder():
Expand Down
19 changes: 16 additions & 3 deletions rasa_nlu/data_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@

logger = logging.getLogger(__name__)

# in some execution environments `reactor.callFromThread` can not be called as it will result in a deadlock as
# the `callFromThread` queues the function to be called by the reactor which only happens after the call to `yield`.
# Unfortunately, the test is blocked there because `app.flush()` needs to be called to allow the fake server to
# respond and change the status of the Deferred on which the client is yielding. Solution: during tests we will set
# this Flag to `False` to directly run the calls instead of wrapping them in `callFromThread`.
DEFERRED_RUN_IN_REACTOR_THREAD = True


def deferred_from_future(future):
"""Converts a concurrent.futures.Future object to a twisted.internet.defer.Deferred obejct.
Expand All @@ -36,9 +43,15 @@ def deferred_from_future(future):
def callback(future):
e = future.exception()
if e:
reactor.callFromThread(d.errback, e)
return
reactor.callFromThread(d.callback, future.result())
if DEFERRED_RUN_IN_REACTOR_THREAD:
reactor.callFromThread(d.errback, e)
else:
d.errback(e)
else:
if DEFERRED_RUN_IN_REACTOR_THREAD:
reactor.callFromThread(d.callback, future.result())
else:
d.callback(future.result())

future.add_done_callback(callback)
return d
Expand Down
82 changes: 0 additions & 82 deletions rasa_nlu/process_logs.py

This file was deleted.

0 comments on commit 74ff207

Please sign in to comment.