igalshilman opened a new pull request #134:
URL: https://github.com/apache/flink-statefun/pull/134
This PR allows registering asynchronous python functions as stateful
functions, by introducing a new handler: `AsyncRequestReplyHandler`.
Invocation of `AsyncRequestReplyHandler` handler produces a future that can
be `await`ed on, which makes this handler usable in completely asynchronous web
frameworks (like `aiohttp`)
Here is an example remote function implementation that uses `aiohttp` with
the new async handler:
```
import aiohttp
import asyncio
...
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
@function.bind("example/hello")
async def hello(context, message):
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://python.org')
context.pack_and_reply(SomeProtobufMessage(html))
from aiohttp import webhandler
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)
```
The changes in this PR are as follows:
* Refactoring common functionally between an async-handler and sync-handler
into a common handler
* add an async handler
* add an example and IT test.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]