Skip to main content

Python actor framework for heterogeneous computing.

Project description


Python actor framework for heterogeneous computing.

PyPI Latest Release Coverage Build Status License

What is actor

Writing parallel and distributed programs is often challenging and requires a lot of time to deal with concurrency issues. Actor model provides a high-level, scalable and robust abstraction for building distributed applications. It provides several benefits:

  • Scalability: Actors easily scale across nodes. The asynchronous, non-blocking nature of actors allows them to handle huge volumes of concurrent tasks efficiently.
  • Concurrency: The actor model abstracts over concurrency, allowing developers to avoid raw threads and locks.
  • Modularity: An actor system decomposes naturally into a collection of actors that can be understood independently. Actor logic is encapsulated within the actor itself.

Why Xoscar

Xoscar implements the actor model in Python and provides user-friendly APIs that offer significant benefits for building applications on heterogeneous hardware:

  • Abstraction over low-level communication details: Xoscar handles all communication between actors transparently, whether on CPUs, GPUs, or across nodes. Developers focus on application logic rather than managing hardware resources and optimizing data transfer.
  • Flexible actor models: Xoscar supports both stateful and stateless actors. Stateful actors ensure thread safety for concurrent systems while stateless actors can handle massive volumes of concurrent messages. Developers choose the appropriate actor model for their needs.
  • Batch method: Xoscar provides a batch interface to significantly improve call efficiency when an actor interface is invoked a large number of times.
  • Advanced debugging support: Xoscar can detect potential issues like deadlocks, long-running calls, and performance bottlenecks that would otherwise be nearly impossible to troubleshoot in a heterogeneous environment.
  • Automated recovery: If an actor fails for any reason, Xoscar will automatically restart it if you want. It can monitor actors and restart them upon failure, enabling fault-tolerant systems.

Overview

architecture.png Xoscar allows you to create multiple actor pools on each worker node, typically binding an actor pool to a CPU core or a GPU card. Xoscar provides allocation policies so that whenever an actor is created, it will be instantiated in the appropriate pool based on the specified policy.

When actors communicate, Xoscar will choose the optimal communication mechanism based on which pools the actors belong to. This allows Xoscar to optimize communication in heterogeneous environments with multiple processing units and accelerators.

Where to get it

The source code is currently hosted on GitHub at: https://github.com/xorbitsai/xoscar

Binary installers for the latest released version are available at the Python Package Index (PyPI).

# PyPI
pip install xoscar

APIs

Here are basic APIs for Xoscar.

Define an actor

import xoscar as xo

# stateful actor, for stateless actor, inherit from xo.StatelessActor
class MyActor(xo.Actor):
    def __init__(self, *args, **kwargs):
        pass

    async def __post_create__(self):
        # called after created
        pass

    async def __pre_destroy__(self):
        # called before destroy
        pass

    def method_a(self, arg_1, arg_2, **kw_1):  # user-defined function
        pass

    async def method_b(self, arg_1, arg_2, **kw_1):  # user-defined async function
        pass

Create an actor

import xoscar as xo

actor_ref = await xo.create_actor(
    MyActor, 1, 2, a=1, b=2,
    address='<ip>:<port>', uid='UniqueActorName')

Get an actor reference

import xoscar as xo

actor_ref = await xo.actor_ref(address, actor_id)

Invoke a method

# send
await actor_ref.method_a.send(1, 2, a=1, b=2)
# equivalent to actor_ref.method_a.send
await actor_ref.method_a(1, 2, a=1, b=2)
# tell, it sends a message asynchronously and does not wait for a response.
await actor_ref.method_a.tell(1, 2, a=1, b=2)

Batch method

Xoscar provides a set of APIs to write batch methods. You can simply add a @extensible decorator to your actor method and create a batch version. All calls wrapped in a batch will be sent together, reducing possible RPC cost.

Define a batch method

import xoscar as xo

class ExampleActor(xo.Actor):
    @xo.extensible
    async def batch_method(self, a, b=None):
        pass

Xoscar also supports creating a batch version of the method:

class ExampleActor(xo.Actor):
    @xo.extensible
    async def batch_method(self, a, b=None):
        raise NotImplementedError  # this will redirect all requests to the batch version

    @batch_method.batch
    async def batch_method(self, args_list, kwargs_list):
        results = []
        for args, kwargs in zip(args_list, kwargs_list):
            a, b = self.batch_method.bind(*args, **kwargs)
            # process the request
            results.append(result)
        return results  # return a list of results

In a batch method, users can define how to more efficiently process a batch of requests.

Invoke a batch method

Calling batch methods is easy. You can use <method_name>.delay to make a batched call and use <method_name>.batch to send them:

ref = await xo.actor_ref(uid='ExampleActor', address='127.0.0.1:13425')
results = await ref.batch_method.batch(
    ref.batch_method.delay(10, b=20),
    ref.batch_method.delay(20),
)

License

Apache 2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xoscar-0.0.8.tar.gz (116.2 kB view hashes)

Uploaded Source

Built Distributions

xoscar-0.0.8-cp311-cp311-win_amd64.whl (509.9 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

xoscar-0.0.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

xoscar-0.0.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

xoscar-0.0.8-cp311-cp311-macosx_10_9_x86_64.whl (634.9 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

xoscar-0.0.8-cp311-cp311-macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

xoscar-0.0.8-cp310-cp310-win_amd64.whl (518.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

xoscar-0.0.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

xoscar-0.0.8-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

xoscar-0.0.8-cp310-cp310-macosx_10_9_x86_64.whl (647.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

xoscar-0.0.8-cp310-cp310-macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

xoscar-0.0.8-cp39-cp39-win_amd64.whl (528.6 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

xoscar-0.0.8-cp39-cp39-win32.whl (470.1 kB view hashes)

Uploaded CPython 3.9 Windows x86

xoscar-0.0.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

xoscar-0.0.8-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

xoscar-0.0.8-cp39-cp39-macosx_10_9_x86_64.whl (656.2 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

xoscar-0.0.8-cp39-cp39-macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

xoscar-0.0.8-cp38-cp38-win_amd64.whl (528.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

xoscar-0.0.8-cp38-cp38-win32.whl (470.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

xoscar-0.0.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

xoscar-0.0.8-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

xoscar-0.0.8-cp38-cp38-macosx_10_9_x86_64.whl (647.9 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

xoscar-0.0.8-cp38-cp38-macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page