GithubHelp home page GithubHelp logo

graalpy-flask's Introduction

Buildpackify-GraalPy

  • your Python code isn't running as awesome as it could be.
  • your data scientists shouldn't be creating Dockerfile for production.
  • you would like to save some money on your application infrastructure.
  • you want to sleep well, instead of worrying about production container security.

This project tries to help.

Paketo makes amazing buildpacks for multiple languages, including Python. For one use case, I recommended using the Paketo buildpack for Python to a customer, to replace their Dockerfile. The buildpack worked, but the resulting container was still multiple GB in size.

We needed another way.

We decided to bring in GraalPy and give it a try. Of course it worked, GraalPy was able to optimize the Python project. But, it didn't help with creating a production-ready, enterprise-grade container.

Friends don't let friends use Dockerfile.

So we used the GraalPy Maven Archetype to generate a project, that we could deploy with Paketo buildpacks.

This project takes Python wraps it with Java and connects them via the polyglot API and creates an OCI image using Paketo Buildpacks.

Prerequisites

Getting Started - Create an optimized OCI Image for Python

pack build buildpackify \
--builder paketobuildpacks/builder-jammy-base \
--buildpack paketo-buildpacks/graalvm \
--buildpack paketo-buildpacks/java \
--env BP_MAVEN_ACTIVE_PROFILES=native \
--env BP_MAVEN_BUILT_ARTIFACT=target/buildpackify-1.0-SNAPSHOT.zip \
--env BP_NATIVE_IMAGE=true \
--env MVN=/layers/paketo-buildpacks_maven/maven/bin/mvn

Now you have an OCI image to run

# Run the OCI image
docker run buildpackify

Q.E.D.

Development Setup

# Initialize the shell with GraalVM and Maven
sdk env install
# Build the GraalPy native image
mvn -Pnative package
# Execute the native version of the Python code: hello.py
./target/buildpackify

Quick recap

The project creates a virtual file system that works like a Python venv but inside the JVM. All of Python config is set up as part of the context with the GraalPy class.

The GraalPy class implements the Hello interface using the Python code in hello.py.

The Java code can access the native version of the Python code using the provided polyglot API

Background Details

These are the steps I took to set up this project. Provided for informational purposes only.

sdk install java 21.0.3-graal
sdk use java 21.0.3-graal
sdk install maven
sdk env init
mvn archetype:generate \
  -DarchetypeGroupId=org.graalvm.python \
  -DarchetypeArtifactId=graalpy-archetype-polyglot-app \
  -DarchetypeVersion=24.0.0
  • groupId: com.dashaun.paketo.graalpy
  • artifactId: buildpackify
  • version: 1.0-SNAPSHOT
  • package: com.dashaun.paketo.graalpy

Now we have a maven project that includes some simple python code and is ready to go!

Info from the archetype generation

This simple project is meant as a jumping off point for a polyglot Python Java application on GraalVM.

To build a standalone native executable you need to use a GraalVM JDK with Native Image and run:

mvn package -Pnative

The additional steps I took to go from Archetype to Buildpackified

  • Added Procfile
  • Added assembly config, xml and plugin

graalpy-flask's People

Watchers

Corby Page avatar

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.