-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathfunctions.py
207 lines (138 loc) · 3.49 KB
/
functions.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright Modal Labs 2022
from __future__ import annotations
import asyncio
import time
from datetime import date
from modal import Stub, asgi_app, method, web_endpoint
from modal.exception import deprecation_warning
SLEEP_DELAY = 0.1
stub = Stub()
@stub.function()
def square(x):
return x * x
@stub.function()
def delay(t):
time.sleep(t)
@stub.function()
async def square_async(x):
await asyncio.sleep(SLEEP_DELAY)
return x * x
@stub.function()
def raises(x):
raise Exception("Failure!")
@stub.function()
def raises_sysexit(x):
raise SystemExit(1)
@stub.function()
def raises_keyboardinterrupt(x):
raise KeyboardInterrupt()
@stub.function()
def gen_n(n):
for i in range(n):
yield i**2
@stub.function()
def gen_n_fail_on_m(n, m):
for i in range(n):
if i == m:
raise Exception("bad")
yield i**2
def deprecated_function(x):
deprecation_warning(date(2000, 1, 1), "This function is deprecated")
return x**2
@stub.function()
@web_endpoint()
def webhook(arg="world"):
return {"hello": arg}
def stream():
for i in range(10):
time.sleep(SLEEP_DELAY)
yield f"{i}..."
@stub.function()
@web_endpoint()
def webhook_streaming():
from fastapi.responses import StreamingResponse
return StreamingResponse(stream())
async def stream_async():
for i in range(10):
await asyncio.sleep(SLEEP_DELAY)
yield f"{i}..."
@stub.function()
@web_endpoint()
async def webhook_streaming_async():
from fastapi.responses import StreamingResponse
return StreamingResponse(stream_async())
if __name__ == "__main__":
raise Exception("This line is not supposed to be reachable")
def gen(n):
for i in range(n):
yield i**2
@stub.function(is_generator=True)
def fun_returning_gen(n):
return gen(n)
@stub.function()
@asgi_app()
def fastapi_app():
from fastapi import FastAPI
web_app = FastAPI()
@web_app.get("/foo")
async def foo(arg="world"):
return {"hello": arg}
return web_app
@stub.cls()
class Cls:
def __init__(self):
self._k = 11
def __enter__(self):
self._k += 100
@method()
def f(self, x):
return self._k * x
@web_endpoint()
def web(self, arg):
return {"ret": arg * self._k}
def _generator(self, x):
yield x**3
@method(is_generator=True)
def generator(self, x):
return self._generator(x)
@stub.function()
def check_sibling_hydration(x):
assert square.is_hydrated()
assert Cls().f.is_hydrated()
assert Cls().web.is_hydrated()
assert Cls().web.web_url
assert Cls().generator.is_hydrated()
assert Cls().generator.is_generator
assert fastapi_app.is_hydrated()
assert fun_returning_gen.is_hydrated()
assert fun_returning_gen.is_generator
@stub.cls()
class ParamCls:
def __init__(self, x: int, y: str) -> None:
self.x = x
self.y = y
@method()
def f(self, z: int):
return f"{self.x} {self.y} {z}"
@method()
def g(self, z):
return self.f.local(z)
@stub.function(allow_concurrent_inputs=5)
def sleep_700_sync(x):
time.sleep(0.7)
return x * x
@stub.function(allow_concurrent_inputs=5)
async def sleep_700_async(x):
await asyncio.sleep(0.7)
return x * x
def unassociated_function(x):
return 100 - x
class BaseCls:
def __enter__(self):
self.x = 2
@method()
def run(self, y):
return self.x * y
@stub.cls()
class DerivedCls(BaseCls):
pass