10000 fix: clarify typehint for callback function by hanxiao · Pull Request #2667 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: clarify typehint for callback function #2667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions .github/2.0/cookbooks/Flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,21 @@ with f, open('my.csv') as fp:

#### Callback Functions

Once a request is made, callback functions are fired. Jina Flow implements a Promise-like interface. You can add
callback functions `on_done`, `on_error`, `on_always` to hook different events. In the example below, our Flow passes
Once a request is returned, callback functions are fired. Jina Flow implements a Promise-like interface. You can add
callback functions `on_done`, `on_error`, `on_always` to hook different events.

In Jina, callback function's first argument is a `jina.types.request.Response` object. Hence, you can annotate the callback function via:

```python
from jina.types.request import Response

def my_callback(rep: Response):
...
```

`Response` object has many attributes, probably the most popular one is `Response.docs`, where you can access all `Document` as an `DocumentArray`.

In the example below, our Flow passes
the message then prints the result when successful. If something goes wrong, it beeps. Finally, the result is written
to `output.txt`.

Expand All @@ -553,16 +566,16 @@ from jina import Document, Flow

def beep(*args):
# make a beep sound
import os
os.system('echo -n "\a";')
import sys
sys.stdout.write('\a')


with Flow().add() as f, open('output.txt', 'w') as fp:
f.post('/',
Document(),
on_done=print,
on_error=beep,
on_always=lambda x: fp.write(x.json()))
on_always=lambda x: x.docs.save(fp))
```

#### Send Parameters
Expand Down
7 changes: 3 additions & 4 deletions jina/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import inspect
import os
from contextlib import nullcontext
from typing import Callable, Union, Optional, Iterator, Iterable, AsyncIterator
from typing import Callable, Union, Optional, Iterator, AsyncIterator

import grpc

Expand All @@ -18,11 +18,10 @@
from ..logging.profile import TimeContext, ProgressBar
from ..parsers import set_client_cli_parser
from ..proto import jina_pb2_grpc
from ..types.request import Request
from ..types.request import Request, Response

InputType = Union[GeneratorSourceType, Callable[..., GeneratorSourceType]]
InputDeleteType = Union[str, Iterable[str], Callable[..., Iterable[str]]]
CallbackFnType = Optional[Callable[..., None]]
CallbackFnType = Optional[Callable[[Response], None]]


class BaseClient:
Expand Down
28 changes: 24 additions & 4 deletions jina/flow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,15 +1062,20 @@ def __iter__(self):
return self._pod_nodes.items().__iter__()

def _show_success_message(self):

if self._pod_nodes['gateway'].args.restful:
header = 'http://'
protocol = 'REST'
else:
header = 'tcp://'
protocol = 'gRPC'

self.logger.success(
f'🎉 Flow is ready to use, accepting {colored(protocol + " request", attrs="bold")}'
)

address_table = [
f'\t🖥️ Local access:\t'
f'\t🏠 Local access:\t'
+ colored(
f'{header}{self.host}:{self.port_expose}', 'cyan', attrs='underline'
),
Expand All @@ -1090,9 +1095,24 @@ def _show_success_message(self):
attrs='underline',
)
)
self.logger.success(
f'🎉 Flow is ready to use, accepting {colored(protocol + " request", attrs="bold")}'
)
if self.args.restful:
address_table.append(
f'\t💬 Swagger UI:\t\t'
+ colored(
f'http://localhost:{self.port_expose}/docs',
'cyan',
attrs='underline',
)
)
address_table.append(
f'\t📚 Redoc:\t\t'
+ colored(
f'http://localhost:{self.port_expose}/redoc',
'cyan',
attrs='underline',
)
)

self.logger.info('\n' + '\n'.join(address_table))

def block(self):
Expand Down
4 changes: 2 additions & 2 deletions jina/helloworld/chatbot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .my_executors import MyTransformer, MyIndexer


def create_chatbot_flow(args):
def _get_flow(args):
"""Ensure the same flow is used in hello world example and system test."""
return (
Flow()
Expand Down Expand Up @@ -55,7 +55,7 @@ def hello_world(args):
# now comes the real work
# load index flow from a YAML file

f = create_chatbot_flow(args)
f = _get_flow(args)

# index it!
with f, open(targets['covid-csv']['filename']) as fp:
Expand Down
7 changes: 0 additions & 7 deletions jina/peapods/runtimes/asyncio/rest/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ def _shutdown():
@app.on_event('startup')
async def startup():
"""Log the host information when start the server."""
default_logger.info(
f'''
Jina REST interface
💬 Swagger UI:\thttp://localhost:{args.port_expose}/docs
📚 Redoc :\thttp://localhost:{args.port_expose}/redoc
'''
)
from jina import __ready_msg__

default_logger.success(__ready_msg__)
Expand Down
4 changes: 2 additions & 2 deletions jina/types/request/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)


class Request(ProtoTypeMixin):
class Request(ProtoTypeMixin, DocsPropertyMixin, GroundtruthPropertyMixin):
"""
:class:`Request` is one of the **primitive data type** in Jina.

Expand Down Expand Up @@ -254,7 +254,7 @@ def parameters(self, value: Dict):
self._pb_body.parameters.update(value)


class Response(Request, DocsPropertyMixin, GroundtruthPropertyMixin):
class Response(Request):
"""
Response is the :class:`Request` object returns from the flow. Right now it shares the same representation as
:class:`Request`. At 0.8.12, :class:`Response` is a simple alias. But it does give a more consistent semantic on
Expand Down
4 changes: 2 additions & 2 deletions tests/system/chatbot/test_chatbot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from jina import Document
from jina.helloworld.chatbot.app import hello_world, create_chatbot_flow
from jina.helloworld.chatbot.app import hello_world, _get_flow
from jina.parsers.helloworld import set_hw_chatbot_parser
from tests import validate_callback

Expand Down Expand Up @@ -39,7 +39,7 @@ def validate_response(resp):
mock_on_done = mocker.Mock()
mock_on_fail = mocker.Mock()

flow = create_chatbot_flow(helloworld_args)
flow = _get_flow(helloworld_args)
with flow as f:
f.index(
inputs=Document(
Expand Down
0