Skip to content

Commit 26e49b5

Browse files
committed
asyn.py Event now has optional delay_ms arg.
1 parent fa86ab4 commit 26e49b5

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

PRIMITIVES.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,18 @@ async def bar(lock):
127127
While the coro `bar` is accessing the resource, other coros will pause at the
128128
`async with lock` statement until the context manager in `bar()` is complete.
129129

130-
Note that MicroPython has a bug in its implementation of asynchronous context
131-
managers: a `return` statement should not be issued in the `async with` block.
132-
See note at end of [this section](./TUTORIAL.md#43-asynchronous-context-managers).
130+
Note that MicroPython had a bug in its implementation of asynchronous context
131+
managers. This is fixed: if you build from source there is no problem. Alas the
132+
fix was too late for release build V1.9.4. If using that build a `return`
133+
statement should not be issued in the `async with` block. See note at end of
134+
[this section](./TUTORIAL.md#43-asynchronous-context-managers).
133135

134136
### 3.2.1 Definition
135137

136138
Constructor: Optional argument `delay_ms` default 0. Sets a delay between
137139
attempts to acquire the lock. In applications with coros needing frequent
138-
scheduling a nonzero value will facilitate this at the expense of latency.
140+
scheduling a nonzero value will reduce the `Lock` object's CPU overhead at the
141+
expense of latency.
139142
Methods:
140143

141144
* `locked` No args. Returns `True` if locked.
@@ -190,10 +193,10 @@ Example of this are in `event_test` and `ack_test` in asyntest.py.
190193

191194
### 3.3.1 Definition
192195

193-
Constructor: takes one optional boolean argument, defaulting False.
194-
* `lp` If `True` and the experimental low priority core.py is installed,
195-
low priority scheduling will be used while awaiting the event. If the standard
196-
version of uasyncio is installed the arg will have no effect.
196+
Constructor: takes one optional integer argument.
197+
* `delay_ms` default 0. While awaiting an event an internal flag is repeatedly
198+
polled. Setting a finite polling interval reduces the task's CPU overhead at
199+
the expense of increased latency.
197200

198201
Synchronous Methods:
199202
* `set` Initiates the event. Optional arg `data`: may be of any type,

TUTORIAL.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,8 @@ value returned by `__aenter__`.
883883

884884
There was a bug in the implementation whereby if an explicit `return` was issued
885885
within an `async with` block, the `__aexit__` method was not called. This was
886-
fixed as of 27th June 2018 [PR 3890](https://github.com/micropython/micropython/pull/3890).
886+
fixed as of 27th June 2018 [PR 3890](https://github.com/micropython/micropython/pull/3890)
887+
but the fix was too late for the current release build (V1.9.4).
887888

888889
###### [Contents](./TUTORIAL.md#contents)
889890

asyn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def release(self):
9696
# When all waiting coros have run
9797
# event.clear() should be issued
9898
class Event():
99-
def __init__(self, lp=False): # Redundant arg retained for compatibility
99+
def __init__(self, delay_ms=0):
100+
self.delay_ms = delay_ms
100101
self.clear()
101102

102103
def clear(self):
@@ -105,7 +106,7 @@ def clear(self):
105106

106107
def __await__(self):
107108
while not self._flag:
108-
yield
109+
await asyncio.sleep_ms(self.delay_ms)
109110

110111
__iter__ = __await__
111112

0 commit comments

Comments
 (0)