forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncweb.py
More file actions
210 lines (161 loc) · 4.69 KB
/
asyncweb.py
File metadata and controls
210 lines (161 loc) · 4.69 KB
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
208
209
210
from _js import Promise
from collections.abc import Coroutine
try:
import browser
except ImportError:
browser = None
def is_promise(prom):
return callable(getattr(prom, "then", None))
def run(coro):
"""
Run a coroutine. The coroutine should yield promise objects with a
``.then(on_success, on_error)`` method.
"""
_Runner(coro)
def spawn(coro):
"""
Run a coroutine. Like run(), but returns a promise that resolves with
the result of the coroutine.
"""
return _coro_promise(coro)
class _Runner:
def __init__(self, coro):
self._send = coro.send
self._throw = coro.throw
# start the coro
self.success(None)
def _run(self, send, arg):
try:
ret = send(arg)
except StopIteration:
return
ret.then(self.success, self.error)
def success(self, res):
self._run(self._send, res)
def error(self, err):
self._run(self._throw, err)
def main(async_func):
"""
A decorator to mark a function as main. This calls run() on the
result of the function, and logs an error that occurs.
"""
run(_main_wrapper(async_func()))
return async_func
async def _main_wrapper(coro):
try:
await coro
except: # noqa: E722
import traceback
import sys
# TODO: sys.stderr on wasm
traceback.print_exc(file=sys.stdout)
def _resolve(prom):
if is_promise(prom):
return prom
elif isinstance(prom, Coroutine):
return _coro_promise(prom)
else:
return Promise.resolve(prom)
class CallbackPromise:
def __init__(self):
self.done = 0
self.__successes = []
self.__errors = []
def then(self, success=None, error=None):
if success and not callable(success):
raise TypeError("success callback must be callable")
if error and not callable(error):
raise TypeError("error callback must be callable")
if not self.done:
if success:
self.__successes.append(success)
if error:
self.__errors.append(error)
return
cb = success if self.done == 1 else error
if cb:
return _call_resolve(cb, self.__result)
else:
return self
def __await__(self):
yield self
def resolve(self, value):
if self.done:
return
self.__result = value
self.done = 1
for f in self.__successes:
f(value)
del self.__successes, self.__errors
def reject(self, err):
if self.done:
return
self.__result = err
self.done = -1
for f in self.__errors:
f(err)
del self.__successes, self.__errors
def _coro_promise(coro):
prom = CallbackPromise()
async def run_coro():
try:
res = await coro
except BaseException as e:
prom.reject(e)
else:
prom.resolve(res)
run(run_coro())
return prom
def _call_resolve(f, arg):
try:
ret = f(arg)
except BaseException as e:
return Promise.reject(e)
else:
return _resolve(ret)
# basically an implementation of Promise.all
def wait_all(proms):
cbs = CallbackPromise()
if not isinstance(proms, (list, tuple)):
proms = tuple(proms)
num_completed = 0
num_proms = len(proms)
if num_proms == 0:
cbs.resolve(())
return cbs
results = [None] * num_proms
# needs to be a separate function for creating a closure in a loop
def register_promise(i, prom):
prom_completed = False
def promise_done(success, res):
nonlocal prom_completed, results, num_completed
if prom_completed or cbs.done:
return
prom_completed = True
if success:
results[i] = res
num_completed += 1
if num_completed == num_proms:
result = tuple(results)
del results
cbs.resolve(result)
else:
del results
cbs.reject(res)
_resolve(prom).then(
lambda res: promise_done(True, res),
lambda err: promise_done(False, err),
)
for i, prom in enumerate(proms):
register_promise(i, prom)
return cbs
if browser:
_settimeout = browser.window.get_prop("setTimeout")
def timeout(ms):
prom = CallbackPromise()
@browser.jsclosure_once
def cb(this):
print("AAA")
prom.resolve(None)
_settimeout.call(cb.detach(), browser.jsfloat(ms))
return prom