Run#

Executor objects can be used directly, just like any regular Python object.

There are two ways of instantiating an Executor object: From a local Python class, and from the Jina Hub.

Executor objects can be used directly, just like a regular Python object. For example:

from jina import Executor, requests, DocumentArray, Document


class MyExec(Executor):
    @requests
    def foo(self, docs, **kwargs):
        for d in docs:
            d.text = 'hello world'


m = MyExec()
da = DocumentArray([Document(text='test')])
m.foo(da)
print(f'Text: {da[0].text}')
Text: hello world

You can pull an Executor from the Jina Hub and use it directly as a Python object. The hub is our marketplace for Executors.

from docarray import Document, DocumentArray, Executor

executor = Executor.from_hub(uri='jinahub://CLIPTextEncoder', install_requirements=True)

docs = DocumentArray(Document(text='hello'))
executor.encode(docs, {})

print(docs.embeddings.shape)
(1, 512)

Run async Executors#

import asyncio
from jina import Executor, requests


class MyExecutor(Executor):
    @requests
    async def foo(self, **kwargs):
        await asyncio.sleep(1.0)
        print(kwargs)


async def main():
    m = MyExecutor()
    call1 = asyncio.create_task(m.foo())
    call2 = asyncio.create_task(m.foo())
    await asyncio.gather(call1, call2)


asyncio.run(main())