Skip to content

Commit 892f98e

Browse files
Added eventloop.Event.
1 parent 83406da commit 892f98e

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

prompt_toolkit/eventloop/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .async_generator import AsyncGeneratorItem, generator_to_async_generator, consume_async_generator
66
from .defaults import create_event_loop, create_asyncio_event_loop, use_asyncio_event_loop, get_event_loop, set_event_loop, run_in_executor, call_from_executor, run_until_complete
77
from .future import Future, InvalidStateError
8+
from .event import Event
89

910
__all__ = [
1011
# Base.
@@ -34,4 +35,7 @@
3435
# Futures.
3536
'Future',
3637
'InvalidStateError',
38+
39+
# Event.
40+
'Event',
3741
]

prompt_toolkit/eventloop/event.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Asyncronous event implementation.
3+
"""
4+
from __future__ import unicode_literals
5+
from .future import Future
6+
7+
__all__ = [
8+
'Event'
9+
]
10+
11+
12+
class Event(object):
13+
"""
14+
Like `asyncio.event`.
15+
16+
The state is intially false.
17+
"""
18+
def __init__(self):
19+
self._state = False
20+
self._waiting_futures = []
21+
22+
def is_set(self):
23+
return self._state
24+
25+
def clear(self):
26+
self._state = False
27+
28+
def set(self):
29+
self._state = True
30+
futures = self._waiting_futures
31+
self._waiting_futures = []
32+
33+
for f in futures:
34+
f.set_result(None)
35+
36+
def wait(self):
37+
if self._state:
38+
return Future.succeed(None)
39+
else:
40+
f = Future()
41+
self._waiting_futures.append(f)
42+
return f

0 commit comments

Comments
 (0)