dispatchrun / timecraft

The WebAssembly Time Machine
https://docs.timecraft.dev
GNU Affero General Public License v3.0
329 stars 7 forks source link

sdk/python: add python app #221

Closed Pryz closed 1 year ago

Pryz commented 1 year ago

This change introduces the timecraft.App in the Python SDK. The main idea is to offer a better UX for Python developers and simplify the access to the different Timecraft primitives.

Here is a very simple example of how to compose an application with this framework:

from timecraft import App
import asyncio
import sys
import pickle

app = App()

def read_response(response):
    return pickle.loads(response.output.body)

@app.function(name="simpletask")
def sometask(a, b):
    return a + b

@app.start()
async def main():
    res = sometask.call(40, 2)

    while not await res.is_done():
        print("waiting...")
        await asyncio.sleep(1)

    resp = await res.result()
    if read_response(resp) != 42:
        raise Exception("bad result")

if __name__ == "__main__":
    asyncio.run(app.run(sys.argv[1:]))