Last active
April 6, 2020 03:21
-
-
Save lauralorenz/966e829e17eb6ab16ab6749dab623460 to your computer and use it in GitHub Desktop.
Project Earth Livestream Code Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
from time import sleep | |
import prefect | |
from prefect import Task, task | |
class AddTask(Task): | |
def __init__(self, default: int, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.default = default | |
def run(self, x: int, y: int = None) -> int: | |
if y is None: | |
y = self.default | |
result = x + y | |
self.logger.info("Result: {}".format(result)) | |
return x + y | |
add = AddTask(default=1) | |
@task | |
def sleepy_task(sleep_seconds: int) -> int: | |
logger = prefect.context.get("logger") | |
logger.info("Sleepy..... {}".format(sleep_seconds)) | |
sleep(sleep_seconds) | |
return randint(1, 100) | |
from prefect import Flow | |
with Flow("My first flow!") as flow: | |
first_int = sleepy_task(5) | |
first_result = add(first_int, y=2) | |
second_int = sleepy_task(8) | |
second_result = add(x=second_int, y=100) | |
third_result = add(x=first_result, y=second_result) | |
if __name__ == "__main__": | |
flow.register("your-project-name-here") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you must have docker installed - go install for your OS at https://www.docker.com/get-started | |
# you must have prefect > 0.10.0 | |
# install with whatever your package manager is - here I've used pip | |
# this was not shown as the demo as I had already installed 0.10.0 | |
# pip install --upgrade "prefect>=0.10.0" | |
# configure your install to use the local backend | |
prefect backend server | |
# start the server | |
prefect server start | |
# in a new terminal window, register your flow | |
python helloworld.py | |
# start an agent | |
prefect agent start | |
# go click the run button for your flow at localhost:8080! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# configure your install to use the Cloud backend | |
prefect backend cloud | |
# authenticate to cloud as per | |
# https://docs.prefect.io/orchestration/tutorial/configure.html#authenticating-with-prefect-cloud | |
# this was not shown in the demo as I was already authenticated | |
# prefect auth login -t $YOUR_PREFECT_USER_AUTH_TOKEN | |
# register your flow. | |
# this was not explicitly shown in the demo but you must have a matching project name in Cloud | |
# for whatever your register() method in this file specifies | |
python helloworld.py | |
# start an agent | |
prefect agent start -t $PREFECT__CLOUD__AGENT__AUTH_TOKEN | |
# go click the run button for your flow at https://cloud.prefect.io! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Annotated code transcript from the demo at https://www.youtube.com/watch?v=k9arDAr1GeA
1-helloworld.py
is the flow I use for the demo of both server and core2-start_core_server.sh
commands were shown first starting at 5:503-flip_to_core.sh
commands were shown following 18:00