Skip to content

Commit 9d5919d

Browse files
committed
uasyncio.core: Implement sleep_ms as an awaitable object instead of coro.
This allows to await it without heap allocation.
1 parent 793bc05 commit 9d5919d

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

uasyncio.core/uasyncio/core.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def run_forever(self):
8787
arg = ret.arg
8888
if isinstance(ret, Sleep):
8989
delay = int(arg * 1000)
90+
if isinstance(ret, SleepMs):
91+
delay = arg
9092
elif isinstance(ret, IORead):
9193
# self.add_reader(ret.obj.fileno(), lambda self, c, f: self.call_soon(c, f), self, cb, ret.obj)
9294
# self.add_reader(ret.obj.fileno(), lambda c, f: self.call_soon(c, f), cb, ret.obj)
@@ -176,8 +178,35 @@ def get_event_loop():
176178
def sleep(secs):
177179
yield int(secs * 1000)
178180

179-
def sleep_ms(ms):
180-
yield ms
181+
# Implementation of sleep_ms awaitable with zero heap memory usage
182+
class SleepMs(SysCall1):
183+
184+
def __init__(self):
185+
self.v = None
186+
self.arg = None
187+
188+
def __call__(self, arg):
189+
self.v = arg
190+
#print("__call__")
191+
return self
192+
193+
def __iter__(self):
194+
#print("__iter__")
195+
return self
196+
197+
def __next__(self):
198+
if self.v is not None:
199+
#print("__next__ syscall enter")
200+
self.arg = self.v
201+
self.v = None
202+
return self
203+
#print("__next__ syscall exit")
204+
_stop_iter.__traceback__ = None
205+
raise _stop_iter
206+
207+
_stop_iter = StopIteration()
208+
sleep_ms = SleepMs()
209+
181210

182211
def coroutine(f):
183212
return f

0 commit comments

Comments
 (0)