yr.invoke

Contents

yr.invoke#

yr.invoke(*args, **kwargs) FunctionProxy[source]#

Decorator for creating stateless remote functions on the openYuanrong system.

This decorator transforms regular Python functions into stateless remote functions that can be executed on the Yuanrong distributed system. When a function is decorated with @yr.invoke, it becomes a StatelessFunction object that provides remote execution capabilities via the .invoke() method.

Note

  • This is a function decorator, use it with @yr.invoke syntax.

  • Due to the performance limitations of the HTTP client, only 100,000 concurrent connections are currently supported per client.

  • The decorated function becomes a StatelessFunction and cannot be called directly.

Parameters:
  • func (FunctionType) – The function that needs to be remotely invoked (when used as @yr.invoke).

  • invoke_options (InvokeOptions, optional) – Invocation options for setting resources and behavior.

  • return_nums (int, optional) – The number of return values of the function, restrictions: greater than 0, this parameter is not recommended to be set too large.

  • initializer (callable, optional) – Initialization function to run before the main function.

Returns:

Returns a StatelessFunction object that wraps the decorated function for remote execution. Data type is StatelessFunction.

Raises:

RuntimeError – If the decorator cannot be applied to objects other than functions.

Examples

Basic decorator usage:
>>> import yr
>>> yr.init()
>>>
>>> @yr.invoke
... def add(a, b):
...     return a + b
>>>
>>> # Function is now a StatelessFunction, call with .invoke()
>>> result_ref = add.invoke(1, 2)
>>> result = yr.get(result_ref)
>>> print(result)  # Output: 3
>>> yr.finalize()
Decorator with parameters:
>>> import yr
>>> yr.init()
>>>
>>> opts = yr.InvokeOptions(cpu=1000, memory=512)
>>> @yr.invoke(invoke_options=opts, return_nums=2)
... def divmod_func(a, b):
...     return divmod(a, b)
>>>
>>> quotient_ref, remainder_ref = divmod_func.invoke(10, 3)
>>> print(yr.get(quotient_ref), yr.get(remainder_ref))  # Output: 3 1
>>> yr.finalize()
Chaining remote function calls:
>>> import yr
>>> yr.init()
>>>
>>> @yr.invoke
... def func1(a):
...     return a + " func1"
>>>
>>> @yr.invoke
... def func2(a):
...     return yr.get(func1.invoke(a)) + " func2"
>>>
>>> result_ref = func2.invoke("hello")
>>> result = yr.get(result_ref)
>>> print(result)  # Output: hello func1 func2
>>> yr.finalize()