imjoy-team / imjoy-rpc

The RPC library used in ImJoy.
MIT License
23 stars 5 forks source link

Hypha sync client #546

Closed oeway closed 1 year ago

oeway commented 1 year ago

This PR creates a wrapper to convert all the async functions into sync:

# pip install "imjoy-rpc>=0.5.25"
import time
from imjoy_rpc.hypha import connect_to_server_sync

server_url = "https://ai.imjoy.io"
server = connect_to_server_sync({"server_url": server_url})

# Now all the functions are sync
token = server.generate_token()
services = server.list_services("public")
print("Public services: #", len(services))

def hello(name):
    print("Hello " + name)
    time.sleep(2)
    return "Hello " + name

server.register_service(
    {
        "name": "Hello World",
        "id": "hello-world",
        "config": {
            "visibility": "protected",
            "run_in_executor": True,
        },
        "hello": hello,
    }
)

Note: this won't work in pyodide/jupyterlite since it doesn't support multi-threading.