Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41e10b7

Browse files
authored
Coroutine support (#25)
1 parent 0c64abc commit 41e10b7

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

fdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def handle(handle_func, loop=None):
2424
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
2525
loop = asyncio.get_event_loop()
2626
try:
27-
cls = runner.JSONProtocol.with_handler(handle_func)
27+
cls = runner.JSONProtocol.with_handler(handle_func, loop=loop)
2828
stdin_pipe_reader = loop.connect_read_pipe(cls, stdin)
2929
loop.run_until_complete(stdin_pipe_reader)
3030
loop.run_forever()

fdk/runner.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@
1717
import ujson
1818
import os
1919
import traceback
20+
import types
2021

2122
from fdk import context
2223
from fdk import errors
2324
from fdk import headers
2425
from fdk import response
2526

2627

28+
def handle_callable(ctx, handle_func, data=None,
29+
loop: asyncio.AbstractEventLoop=None):
30+
r = handle_func(ctx, data=data, loop=loop)
31+
32+
if isinstance(r, types.CoroutineType):
33+
print("function appeared to be a coroutine, awaiting...",
34+
file=sys.stderr, flush=True)
35+
return loop.run_until_complete(r)
36+
37+
return r
38+
39+
2740
def from_request(handle_func, incoming_request, loop=None):
2841
print("request parsed", file=sys.stderr, flush=True)
2942
json_headers = headers.GoLikeHeaders(
@@ -39,11 +52,10 @@ def from_request(handle_func, incoming_request, loop=None):
3952
config=os.environ, headers=json_headers)
4053

4154
print("context allocated", file=sys.stderr, flush=True)
42-
4355
print("starting the function", file=sys.stderr, flush=True)
4456
print(incoming_request.get("body"), file=sys.stderr, flush=True)
45-
response_data = handle_func(
46-
ctx, data=incoming_request.get("body"), loop=loop)
57+
response_data = handle_callable(
58+
ctx, handle_func, data=incoming_request.get("body"), loop=loop)
4759

4860
if isinstance(response_data, response.RawResponse):
4961
return response_data

fdk/tests/test_json.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
import asyncio
1516
import json
1617
import testtools
18+
import uvloop
1719

1820
from fdk import runner
1921
from fdk import response
@@ -36,6 +38,10 @@ def expectioner(ctx, data=None, loop=None):
3638
raise Exception("custom_error")
3739

3840

41+
async def coroutine_func(ctx, data=None, loop=None):
42+
return "OK"
43+
44+
3945
class TestJSONRequestParser(testtools.TestCase):
4046

4147
def setUp(self):
@@ -69,3 +75,12 @@ def test_errored_func(self):
6975
r = runner.handle_request(expectioner, in_bytes)
7076
self.assertIsNotNone(r)
7177
self.assertEqual(500, r.status())
78+
79+
def test_corotuine_func(self):
80+
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
81+
loop = asyncio.get_event_loop()
82+
in_bytes = data.raw_request_without_body.encode('utf8')
83+
r = runner.handle_request(coroutine_func, in_bytes, loop=loop)
84+
self.assertIsNotNone(r)
85+
self.assertEqual(200, r.status())
86+
self.assertIn("OK", r.body())

realease.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
4+
git checkout -b v${FDK_VERSION}-stable
5+
PBR_VERSION=${FDK_VERSION} python setup.py sdist bdist_wheel
6+
twine upload dist/fdk-${FDK_VERSION}*

0 commit comments

Comments
 (0)