Pass keyword arguments to thread executor in the correct way (Fixes #195)
This commit is contained in:
@@ -13,6 +13,7 @@ import time
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from inspect import iscoroutinefunction, iscoroutine
|
from inspect import iscoroutinefunction, iscoroutine
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
async def invoke_handler(handler, *args, **kwargs):
|
async def invoke_handler(handler, *args, **kwargs):
|
||||||
"""Invoke a handler and return the result.
|
"""Invoke a handler and return the result.
|
||||||
@@ -23,7 +24,7 @@ try:
|
|||||||
ret = await handler(*args, **kwargs)
|
ret = await handler(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
ret = await asyncio.get_running_loop().run_in_executor(
|
ret = await asyncio.get_running_loop().run_in_executor(
|
||||||
None, handler, *args, **kwargs)
|
None, partial(handler, *args, **kwargs))
|
||||||
return ret
|
return ret
|
||||||
except ImportError: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
def iscoroutine(coro):
|
def iscoroutine(coro):
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
async def index2(req):
|
async def index2(req):
|
||||||
return 'foo-async'
|
return 'foo-async'
|
||||||
|
|
||||||
|
@app.route('/arg/<id>')
|
||||||
|
def index3(req, id):
|
||||||
|
return id
|
||||||
|
|
||||||
|
@app.route('/arg/async/<id>')
|
||||||
|
async def index4(req, id):
|
||||||
|
return f'async-{id}'
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
res = self._run(client.get('/'))
|
res = self._run(client.get('/'))
|
||||||
@@ -45,6 +53,24 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'foo-async')
|
self.assertEqual(res.body, b'foo-async')
|
||||||
self.assertEqual(res.json, None)
|
self.assertEqual(res.json, None)
|
||||||
|
|
||||||
|
res = self._run(client.get('/arg/123'))
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers['Content-Type'],
|
||||||
|
'text/plain; charset=UTF-8')
|
||||||
|
self.assertEqual(res.headers['Content-Length'], '3')
|
||||||
|
self.assertEqual(res.text, '123')
|
||||||
|
self.assertEqual(res.body, b'123')
|
||||||
|
self.assertEqual(res.json, None)
|
||||||
|
|
||||||
|
res = self._run(client.get('/arg/async/123'))
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers['Content-Type'],
|
||||||
|
'text/plain; charset=UTF-8')
|
||||||
|
self.assertEqual(res.headers['Content-Length'], '9')
|
||||||
|
self.assertEqual(res.text, 'async-123')
|
||||||
|
self.assertEqual(res.body, b'async-123')
|
||||||
|
self.assertEqual(res.json, None)
|
||||||
|
|
||||||
def test_post_request(self):
|
def test_post_request(self):
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user