You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2020/09/25 05:59:44 UTC

[flink-statefun] 01/02: [FLINK-19399][doc][python] Document AsyncRequestReplyHandler

This is an automated email from the ASF dual-hosted git repository.

tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git

commit 8d2e907f4d15b8b8d295beb84ca06ad764148023
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Thu Sep 24 16:53:04 2020 +0200

    [FLINK-19399][doc][python] Document AsyncRequestReplyHandler
    
    This closes #162.
---
 docs/sdk/python.md | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/docs/sdk/python.md b/docs/sdk/python.md
index ed74c30..7c00775 100644
--- a/docs/sdk/python.md
+++ b/docs/sdk/python.md
@@ -239,6 +239,36 @@ if __name__ == "__main__":
 	app.run()
 {% endhighlight %}
 
+
+### Serving Asynchronous Functions 
+
+The Python SDK ships with an additional handler, ``AsyncRequestReplyHandler``, that supports Python's awaitable functions (coroutines).
+This handler can be used with asynchronous Python frameworks, for example [aiohttp](https://docs.aiohttp.org/en/stable/).
+
+{% highlight python %}
+@functions.bind("example/hello")
+async def hello(context, message):
+    response = await compute_greeting(message)
+    context.reply(response)
+
+from aiohttp import web
+
+handler = AsyncRequestReplyHandler(functions)
+
+async def handle(request):
+    req = await request.read()
+    res = await handler(req)
+    return web.Response(body=res, content_type="application/octet-stream")
+
+app = web.Application()
+app.add_routes([web.post('/statefun', handle)])
+
+if __name__ == '__main__':
+    web.run_app(app, port=5000)
+
+{% endhighlight %}
+
+
 ## Context Reference
 
 The ``context`` object passed to each function has the following attributes / methods.