Skip to main content

Sorna API Client Library

Project description

PyPI Python Versions Travis Build Status AppVeyor Build Status Code Coverage

The API client library for Sorna

Usage

You should set the access key and secret key as environment variables to use the API. Grab your keypair from cloud.sorna.io or your cluster admin.

export SORNA_ACCESS_KEY=...
export SORNA_SECRET_KEY=...

# optional (for local clusters)
export SORNA_ENDPOINT="https://my-precious-cluster/"

Command-line Interface

Use sorna.cli module with run command.

To run the code specified in the command line directly, use -c option to pass the code string.

$ python -m sorna.cli run python3 -c "print('hello world')"
∙ Client session token: d3694dda6e5a9f1e5c718e07bba291a9
✔ Kernel (ID: zuF1OzMIhFknyjUl7Apbvg) is ready.
hello world
✔ Cleaned up the kernel.

For more large programs, you may upload multiple files and then build & execute them. The below is a simple example to run a C program.

$ python -m sorna.cli run c main.c mylib.c mylib.h
∙ Client session token: 1c352a572bc751a81d1f812186093c47
✔ Kernel (ID: kJ6CgWR7Tz3_v2WsDHOwLQ) is ready.
✔ Uploading done.
✔ Build finished.
myvalue is 42
✔ Cleaned up the kernel.

Please refer the --help manual provided by the run command.

Synchronous API

from sorna.kernel import Kernel

kern = Kernel.get_or_create('lua5', client_token='abc')
result = kern.execute('print("hello world")', mode='query')
print(result['console'])
kern.destroy()

You need to take care of client_token because it determines whether to reuse kernel sessions or not. Sorna cloud has a timeout so that it terminates long-idle kernel sessions, but within the timeout, any kernel creation requests with the same client_token let Sorna cloud to reuse the kernel.

Asynchronous API

import asyncio
from sorna.asyncio.kernel import AsyncKernel

async def main():
    kern = await AsyncKernel.get_or_create('lua5', client_token='abc')
    result = await kern.execute('print("hello world")', mode='query')
    print(result['console'])
    await kern.destroy()

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.close()

All the methods of AsyncKernel objects are exactly same to the synchronous version, except that they are coroutines.

Additionally, AsyncKernel offers async-only method stream_pty(). It returns a StreamPty object which allows you to access a pseudo-tty of the kernel. StreamPty works like an async-generator and provides methods to send stdin inputs as well as resize the terminal.

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

sorna-client-0.9.4.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

sorna_client-0.9.4-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file sorna-client-0.9.4.tar.gz.

File metadata

File hashes

Hashes for sorna-client-0.9.4.tar.gz
Algorithm Hash digest
SHA256 e8b0ad1ce5151e06356c34dcd59c9d08d5809f6315866911dd2742dbb914d9bc
MD5 a8879674e155ad4b40219954c2fe7771
BLAKE2b-256 ad433ad35c16b7f8d15b2d69084cae023ebc0c20b81d3d55ff13cc1068651385

See more details on using hashes here.

File details

Details for the file sorna_client-0.9.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sorna_client-0.9.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5d6f39da415c0b6ab53f417cc121f10129d80fbd17f26bb2a664437c39eda3e3
MD5 1d03396628d48a6c9cee85227e72304a
BLAKE2b-256 a99612b9d9e2faf51d04fca18c33b6d2bf21389c28ed01ea72565916230acb7a

See more details on using hashes here.

Supported by

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