forked from modal-labs/modal-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world.py
103 lines (87 loc) · 2.89 KB
/
hello_world.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# # Hello, world!
#
# This is a trivial example of a Modal function, but it illustrates a few features:
#
# * You can print things to stdout and stderr.
# * You can return data.
# * You can map over a function.
#
# ## Import Modal and define the app
#
# Let's start with the top level imports.
# You need to import Modal and define the app.
# A stub is an object that defines everything that will be run.
import sys
import modal
stub = modal.Stub("example-hello-world")
# ## Defining a function
#
# Here we define a Modal function using the `modal.function` decorator.
# The body of the function will automatically be run remotely.
# This particular function is pretty silly: it just prints "hello"
# and "world" alternatingly to standard out and standard error.
@stub.function()
def f(i):
if i % 2 == 0:
print("hello", i)
else:
print("world", i, file=sys.stderr)
return i * i
# ## Running it
#
# Finally, let's actually invoke it.
# We put this invocation code inside a `@stub.local_entrypoint()`.
# This is because this module will be imported in the cloud, and we don't want
# this code to be executed a second time in the cloud.
#
# Run `modal run hello_world.py` and the `@stub.local_entrypoint()` decorator will handle
# starting the Modal app and then executing the wrapped function body.
#
# Inside the `main()` function body, we are calling the function `f` in three ways:
#
# 1 As a simple local call, `f(1000)`
# 2. As a simple *remote* call `f.remote(1000)`
# 3. By mapping over the integers `0..19`
@stub.local_entrypoint()
def main():
# Call the function locally.
print(f.local(1000))
# Call the function remotely.
print(f.remote(1000))
# Parallel map.
total = 0
for ret in f.map(range(20)):
total += ret
print(total)
# ## What happens?
#
# When you do `.remote` on function `f`, Modal will execute `f` **in the cloud,**
# not locally on your computer. It will take the code, put it inside a
# container, run it, and stream all the output back to your local
# computer.
#
# Try doing one of these things next.
#
# ### Change the code and run again
#
# For instance, change the `print` statement in the function `f`.
# You can see that the latest code is always run.
#
# Modal's goal is to make running code in the cloud feel like you're
# running code locally. You don't need to run any commands to rebuild,
# push containers, or go to a web UI to download logs.
#
# ### Map over a larger dataset
#
# Change the map range from 20 to some large number. You can see that
# Modal will create and run more containers in parallel.
#
# The function `f` is obviously silly and doesn't do much, but you could
# imagine something more significant, like:
#
# * Training a machine learning model
# * Transcoding media
# * Backtesting a trading algorithm.
#
# Modal lets you parallelize that operation trivially by running hundreds or
# thousands of containers in the cloud.