Skip to main content

No project description provided

Project description

:hibiscus: Petal - Flask, for gRPC services.

Petal reduces the boilerplate required to write and maintain gRPC based services whilst ensuring your service code is always in sync with your definitions. It aims to be a somewhat opinionated gRPC framework. This is very, very early code.

Installation and features

pip install petal

Right now:

  • Define your service methods as simple Python functions
  • Ensure they are up to date with service definitions by utilizing type annotations
  • Build your proto definitions and run your app with a petal command
  • Share compiled protobuf definitions as Python packages.
  • Streaming requests and responses

Future features:

  • Autoreloading during development
  • Structured logging
  • Some form of plugin architecture
  • Distributed tracing
  • A testing client
  • AsyncIO support

Hello world example:

from petal import Service
from example.protobuf.greeter_pb2 import HelloReply, HelloRequest

service = Service(__name__)

@service.grpc()
def say_hello(request: HelloRequest) -> HelloReply:
    return HelloReply(message=f'Hello {request.name}')

Tutorial: Creating a petal app

Lets create a Hello World Petal app. First lets define an entirely useless service. Place the file below in hello_world/protobuf/service.proto:

syntax = "proto3";
package hello_world.protobuf.service;

service HelloWorld {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
}

message HelloReply {
}

Then run petal build hello_world.

Next, write our service definitions. Create a hello_world/__init__.py file and add some imports:

from petal import Service
from hello_world.protobuf.service_pb2 import HelloReply, HelloRequest

service = Service(__name__)

This imports the request and response types our service will need. Next we need to fill in the SayHello method:

@service.grpc()
def say_hello(request: HelloRequest) -> HelloReply:
    return HelloReply()

And finally, run the service by executing petal run hello_world.

Now if we modify the service by adding a new method:

message HelloAgain {}

service HelloWorld {
  rpc SayHelloAgain (HelloRequest) returns (HelloAgain) {}
}

And run petal build hello_world && petal run hello_world, we get an error:

$ petal run hello_world
Error: Cannot find a suitable method for GRPC method SayHelloAgain
Please ensure one exists in your service code, or use service.grpc(name=NAME) to define one.

Petal is ensuring we have the correct Python methods defined for all our service functions. We can fix this by defining one:

from hello_world.protobuf.service_pb2 import HelloAgain

@service.grpc()
def say_hello_again(request: HelloRequest) -> HelloAgain:
    return HelloAgain()

Now running petal serve hello_world will work.

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

petal-0.0.2.post2.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

petal-0.0.2.post2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file petal-0.0.2.post2.tar.gz.

File metadata

  • Download URL: petal-0.0.2.post2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for petal-0.0.2.post2.tar.gz
Algorithm Hash digest
SHA256 94004689fb3d3e3cbbc3d0d4680459232446c47a028f123e4221a632eeb908f0
MD5 8362c248ca09a6bba0561e5676c29df1
BLAKE2b-256 5d6495480d3dee916991d0344d0c82d061599cf1cf644eefcbb5126770d7f194

See more details on using hashes here.

File details

Details for the file petal-0.0.2.post2-py3-none-any.whl.

File metadata

  • Download URL: petal-0.0.2.post2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for petal-0.0.2.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 5a25b2401d2f0f8ddab943938966f7cf8378fa3962b3ee2e9ea592c8a1f544f4
MD5 a68b16ee63c4c293c9b7607bb43b2c73
BLAKE2b-256 f1bb02a0a6ddcb895eb4b51b5e8d1f4d174d41fe8b652e7e3899d348ac4e0dfb

See more details on using hashes here.

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