jina.clients.asyncioΒΆ
-
class
jina.clients.asyncio.
AsyncClient
(args)[source]ΒΆ Bases:
jina.clients.base.BaseClient
AsyncClient
is the asynchronous version of theClient
. They share the same interface, except inAsyncClient
train()
,index()
,search()
methods are coroutines (i.e. declared with the async/await syntax), simply calling them will not schedule them to be executed. To actually run a coroutine, user need to put them in an eventloop, e.g. viaasyncio.run()
,asyncio.create_task()
.AsyncClient
can be very useful in the integration settings, where Jina/Flow/Client is NOT the main logic, but rather served as a part of other program. In this case, users often do not want to let Jina control theasyncio.eventloop
. On contrary,Client
is controlling and wrapping the eventloop internally, making the Client looks synchronous from outside.For example, say you have the Flow running in remote. You want to use Client to connect to it do some index and search, but meanwhile you have some other IO-bounded jobs and want to do them concurrently. You can use
AsyncClient
,from jina.clients.asyncio import AsyncClient ac = AsyncClient(...) async def jina_client_query(): await ac.search(...) async def heavylifting(): await other_library.download_big_files(...) async def concurrent_main(): await asyncio.gather(jina_client_query(), heavylifting()) if __name__ == '__main__': # under python asyncio.run(concurrent_main())
One can think of
Client
as Jina-managed eventloop, whereasAsyncClient
is self-managed eventloop.- Parameters
args (
Namespace
) β args provided by the CLI
-
train
(input_fn=None, on_done=None, on_error=None, on_always=None, **kwargs)[source]ΒΆ - Parameters
input_fn (
Union
[Iterator
[Union
[~DocumentContentType, ~DocumentSourceType,Tuple
[~DocumentContentType, ~DocumentContentType],Tuple
[~DocumentSourceType, ~DocumentSourceType]]],Callable
[β¦,Iterator
[Union
[~DocumentContentType, ~DocumentSourceType,Tuple
[~DocumentContentType, ~DocumentContentType],Tuple
[~DocumentSourceType, ~DocumentSourceType]]]],None
]) β the input function that generates the contenton_done (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is resolved.on_error (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is rejected.on_always (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is is either resolved or rejected.kwargs β
- Return type
None
- Returns
-
search
(input_fn=None, on_done=None, on_error=None, on_always=None, **kwargs)[source]ΒΆ - Parameters
input_fn (
Union
[Iterator
[Union
[~DocumentContentType, ~DocumentSourceType,Tuple
[~DocumentContentType, ~DocumentContentType],Tuple
[~DocumentSourceType, ~DocumentSourceType]]],Callable
[β¦,Iterator
[Union
[~DocumentContentType, ~DocumentSourceType,Tuple
[~DocumentContentType, ~DocumentContentType],Tuple
[~DocumentSourceType, ~DocumentSourceType]]]],None
]) β the input function that generates the contenton_done (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is resolved.on_error (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is rejected.on_always (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is is either resolved or rejected.kwargs β
- Return type
None
- Returns
-
index
(input_fn=None, on_done=None, on_error=None, on_always=None, **kwargs)[source]ΒΆ - Parameters
input_fn (
Union
[Iterator
[Union
[~DocumentContentType, ~DocumentSourceType,Tuple
[~DocumentContentType, ~DocumentContentType],Tuple
[~DocumentSourceType, ~DocumentSourceType]]],Callable
[β¦,Iterator
[Union
[~DocumentContentType, ~DocumentSourceType,Tuple
[~DocumentContentType, ~DocumentContentType],Tuple
[~DocumentSourceType, ~DocumentSourceType]]]],None
]) β the input function that generates the contenton_done (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is resolved.on_error (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is rejected.on_always (
Optional
[Callable
[β¦,None
]]) β the function to be called when theRequest
object is is either resolved or rejected.kwargs β
- Return type
None
- Returns
-
class
jina.clients.asyncio.
AsyncWebSocketClient
(args)[source]ΒΆ Bases:
jina.clients.asyncio.AsyncClient
,jina.clients.websockets.WebSocketClientMixin
AsyncWebSocketClient
is the asynchronous version of theWebSocketClient
. They share the same interface, except inAsyncWebSocketClient
train()
,index()
,search()
methods are coroutines (i.e. declared with the async/await syntax), simply calling them will not schedule them to be executed. To actually run a coroutine, user need to put them in an eventloop, e.g. viaasyncio.run()
,asyncio.create_task()
.AsyncWebSocketClient
can be very useful in the integration settings, where Jina/Flow/Client is NOT the main logic, but rather served as a part of other program. In this case, users often do not want to let Jina control theasyncio.eventloop
. On contrary,WebSocketClient
is controlling and wrapping the eventloop internally, making the Client looks synchronous from outside.For example, say you have the Flow running in remote. You want to use Client to connect to it do some index and search, but meanwhile you have some other IO-bounded jobs and want to do them concurrently. You can use
AsyncWebSocketClient
,from jina.clients.asyncio import AsyncWebSocketClient ac = AsyncWebSocketClient(...) async def jina_client_query(): await ac.search(...) async def heavylifting(): await other_library.download_big_files(...) async def concurrent_main(): await asyncio.gather(jina_client_query(), heavylifting()) if __name__ == '__main__': # under python asyncio.run(concurrent_main())
One can think of
WebSocketClient
as Jina-managed eventloop, whereasAsyncWebSocketClient
is self-managed eventloop.- Parameters
args (
Namespace
) β args provided by the CLI