invent-framework / invent

Express yourself with code.
https://invent-framework.github.io/
Apache License 2.0
108 stars 12 forks source link

Tasks (core concept) #69

Open ntoll opened 1 month ago

ntoll commented 1 month ago

In Invent parlance, a Task is simply a promise/deferred/future type thing that represents some computation that is yet to produce a result.

We need to clearly specify how this works, in two ways:

  1. At the API layer (from the outside in, as a coder writing Invent apps in Python), so we nail down the concepts and story of a Task.
  2. How this is revealed to non-Python / tooling based views of the Invent application.

First rough thoughts:

For the sake of simplicity, a Task is a fire-and-forget computation whose result ends up as a key/value pair in the datastore. Should a user wish to react to the result of the Task they should subscribe to the key in the datastore. A Task is a non-blocking (await-able) Python function whose return value is put into the datastore against the specified key.

from invent import Task

async def stuff_to_do(x, y):
    ... do non-blocking stuff with x and y...
    return result

t = Task(key="result_stuff", function=stuff_to_do, x=1, y=2, ... other args for the function ...)
# Inside the go() method is an asynio.create_task to schedule the running of stuff_to_do with the given args.
t.go()

# At some point in the future, the datastore["result_stuff"] will be set to the return value of the stuff_to_do
# function. This is all wrapped / taken care of by the Task instance.