Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #14 from eplaut/feature/async_server
Browse files Browse the repository at this point in the history
Server: add async run
  • Loading branch information
eplaut committed Jun 23, 2016
2 parents d91ea60 + e6bc42b commit 195cd7b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 7 additions & 0 deletions butler/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
from flask import Flask
from logbook import debug

Expand Down Expand Up @@ -55,6 +56,12 @@ def run(self, *args, **kwargs):
self._update_app_paramters(*args, **kwargs)
self._app.run(host=self.host, port=self.port, *self.args, **self.kwargs)

def run_async(self, *args, **kwargs):
"""Same as run, but async."""
t = threading.Thread(target=self.run, args=args, kwargs=kwargs)
t.daemon = True
t.start()

def _update_app_paramters(self, *args, **kwargs):
"""Parse `run` function parameters and updates `host` and `port` properties."""
args = list(args) # args is tuple, which is immutable
Expand Down
4 changes: 1 addition & 3 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def post_test_params(self):
def butler_client():
butler_server = ButlerTest.Server('http://localhost:8888')
butler_client = ButlerTest.Client('http://localhost:8888')
t = threading.Thread(target=butler_server.run)
t.daemon = True
t.start()
butler_server.run_async()
time.sleep(1)

def stop_server():
Expand Down
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ def test_client_var(butler_client):

r = butler_client.get_test_defaultvar(200)
assert r.content.decode("utf-8") == 'test 200'

def test_client_not_exits_function(butler_client):
with slash.assert_raises(AttributeError):
butler_client.not_exits_function()

def test_client_unsupported_function(butler_client):
with slash.assert_raises(AttributeError):
butler_client.init_functions()

0 comments on commit 195cd7b

Please sign in to comment.