Code Monkey home page Code Monkey logo

lithops_actor_model's Introduction

Lithops and actor model

About the project

The actor model is a math model for concurrent computations, where the ๐Ÿง” actor symbolizes the universal primitive of concurrent computation.

For this project, I will showcase four examples where Lithops (Python-based library) can work using the actor model. Lithops has three execution modes:

  1. Localhost
  2. Serverless
  3. Standalone

This project is a technical demonstration of what you can achieve with serverless functions and how to adapt our code using the actor model.

Table of Contents
  1. Getting Started
  2. Usage
  3. Built With
  4. Redis
  5. Terraform for provisioning Redis (optional)
  6. License
  7. Contact

Actor model implementation

The code related to the actor model implementation within Lithops was based on this source code: https://github.com/danielBCN/lithops-actors

The author is Daniel Barcelona: https://github.com/danielBCN

Code: ./actor_model/director.py

(back to top)

Getting started

Prerequisites

Creating a virtual environment, activating it, and installing the necessary packages.

# Create Python's virtual environment
python3 -m venv .venv
# Activate Python's virtual environment
source .venv/bin/activate
# Upgrade pip to the latest version
python -m pip install --upgrade pip

(back to top)

Installation

# Production
python -m pip install -r requirements.txt
# Development
# python -m pip install -r requirements-dev.txt

(back to top)

Uninstall

Exit Python's virtual environment.

# Exit Python's virtual environment
deactivate
# Remove Python's virtual environment folder
rm -rf .venv/

(back to top)

Usage

  1. Counter: python actor_model/counter.py
  2. Ping pong: python actor_model/ping_pong.py
  3. Bank account: python actor_model/bank_account.py
  4. Bank account v2: python actor_model/bank_account_v2.py
  5. MNIST: python actor_model/mnist.py

View logs after each run.

lithops logs poll

(back to top)

MNIST

asciicast

The datasets belong to:

  • Yann LeCun, Courant Institute, NYU
  • Corinna Cortes, Google Labs, New York
  • Christopher J.C. Burges, Microsoft Research, Redmond

http://yann.lecun.com/exdb/mnist/

The files have been decompressed and stored within ./datasets/:

  • Test set images: t10k-images-idx3-ubyte
  • Test set labels: t10k-labels-idx1-ubyte
  • Training set images: train-images-idx3-ubyte
  • Training set labels: train-images-idx1-ubyte

The source code for MNIST with NumPy has been adapted from this source: https://github.com/karynaur/mnist-from-numpy/blob/main/mnist.py

The author is Aditya Srinivas Menon: https://github.com/karynaur

(back to top)

Built with

https://github.com/lithops-cloud/lithops

Configuration file for serverless mode:

# Configuration file example for Lithops serverless mode
cp .lithops_config.example .lithops_config

(back to top)

Serverless mode

Lithops configuration: Serverless mode

Compute backend

Lithops configuration: AWS Lambda

Storage backend

  1. The first iteration will use Amazon S3 (Amazon Simple Storage Service) as a cloud object storage.

    Lithops configuration: AWS S3

  2. The subsequent runs will use Redis, due to Redis Stack nature of offering different data structures.

    Lithops configuration: Redis

(back to top)

Redis

In my particular case, I'm using DigitalOcean's Redis service as it is relatively easy to spin up a new instance and destroy it afterwards.

The RESP.app documentation gives a wonderful insight of the different solutions provided by different cloud providers: https://docs.resp.app/en/latest/quick-start/

(back to top)

Troubleshooting Redis

  • The main reason against using AWS ElastiCache (Amazon's managed Redis), is that you can only access it within your VPC (Virtual Private Cloud). Instructions are provided to circumvent these restrictions.
  • Azure Cache for Redis can be accessed from outside of Azure's VPC but Terraform's integration with Azure is not quite stable for me.
    • The port number is usually 6380 instead of the default 6379 port.
  • DigitalOcean uses a different port such as 25061.

Redis out of the box supports 16 databases, 0 is the default one.

To delete the current database run the following command.

flushdb

(back to top)

Terraform for provisioning Redis (optional)

Terraform by HashiCorp is used for IaC (Infrastructure as Code). I've included an example for DigitalOcean managed Redis instance.

This Redis instance has ๐Ÿ”’ encryption at rest and in transit: https://docs.digitalocean.com/products/databases/redis/how-to/secure/

  1. Install Terraform CLI: https://learn.hashicorp.com/tutorials/terraform/install-cli

  2. Install Terraform providers.

    # Install providers
    terraform init
  3. Create a variable definition file for DigitalOcean's personal access token.

    # File with DigitalOcean personal access token ignored by GIT due to .gitignore
    cat << EOF >> digital-ocean.tfvars
    digital_ocean_token = ""
    EOF
  4. Use the variable definition file to spin up the Redis instance.

    # Use variable definitions and create an output file with the changes needed for Terraform apply
    terraform plan --var-file=digital-ocean.tfvars --input=false --out lithops.tfplan
    # DigitalOcean will now spin a managed Redis instance
    terraform apply "lithops.tfplan"
  5. Retrieve Redis configuration.

    # After several minutes retrieve host, port, and password
    terraform show --json
  6. Destroy the Redis instance to keep costs low.

    # Destroy managed Redis instance
    terraform destroy --var-file=digital-ocean.tfvars

(back to top)

Cheat sheet for Terraform CLI

Documentation: https://www.terraform.io/cli/commands

# Install providers
terraform init
# Format code
terraform fmt .
# Validate Terraform code
terraform validate
# Dry run
terraform plan
# Install
terraform apply
# Uninstall
terraform destroy
# View configuration and piping results to jq (Command-line JSON processor) https://github.com/stedolan/jq
terraform show --json | jq .

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Michael A. Johnson Lucas - mjohnson(at)uoc.edu

Project Link: https://github.com/ephod/lithops_actor_model

(back to top)

lithops_actor_model's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.