mirror of https://github.com/nocturn9x/giambio.git
Some minor fixes (though issues still persist), added the giambio.core.EventLoop.schedule() method
This commit is contained in:
parent
40b304f09e
commit
63b90dcfb0
|
@ -5,25 +5,29 @@ loop = giambio.EventLoop()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
What works and what does not
|
What works and what does not (21st March 2020 10:33 AM)
|
||||||
|
|
||||||
- Run tasks concurrently: V
|
- Run tasks concurrently: V
|
||||||
- Join mechanism: V
|
- Join mechanism: V
|
||||||
- Sleep mechanism: V
|
- Sleep mechanism: V
|
||||||
- Cancellation mechanism: V
|
- Cancellation mechanism: X Note: giambio.exceptions.CancelledError is raised inside the parent task instead of the child one, probably related to some f*ck ups with the value of EventLoop.running, need to investigate
|
||||||
- Exception propagation: V
|
- Exception propagation: V
|
||||||
- Concurrent I/O: X Note: I/O would work only when a task is joined (weird)
|
- Concurrent I/O: X Note: I/O would work only when a task is joined (weird)
|
||||||
- Return values of coroutines: X Note: Return values ARE actually stored in task objects properly, but are messed up later when joining tasks
|
- Return values of coroutines: X Note: Return values ARE actually stored in task objects properly, but are messed up later when joining tasks
|
||||||
|
- Scheduling tasks for future execution: V
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def countdown(n):
|
async def countdown(n):
|
||||||
while n > 0:
|
try:
|
||||||
print(f"Down {n}")
|
while n > 0:
|
||||||
n -= 1
|
print(f"Down {n}")
|
||||||
await giambio.sleep(1)
|
n -= 1
|
||||||
return "Count DOWN over"
|
await giambio.sleep(1)
|
||||||
|
print("Countdown over")
|
||||||
|
return "Count DOWN over"
|
||||||
|
except CancelledError:
|
||||||
|
print("countdown cancelled!")
|
||||||
|
|
||||||
async def count(stop, step=1):
|
async def count(stop, step=1):
|
||||||
x = 0
|
x = 0
|
||||||
|
@ -31,13 +35,16 @@ async def count(stop, step=1):
|
||||||
print(f"Up {x}")
|
print(f"Up {x}")
|
||||||
x += step
|
x += step
|
||||||
await giambio.sleep(step)
|
await giambio.sleep(step)
|
||||||
|
print("Countup over")
|
||||||
return "Count UP over"
|
return "Count UP over"
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
print("Spawning countdown immediately, scheduling count for 2 secs from now")
|
||||||
task = loop.spawn(countdown(8))
|
task = loop.spawn(countdown(8))
|
||||||
task1 = loop.spawn(count(8, 2))
|
task1 = loop.schedule(count(12, 2), 2)
|
||||||
print(await giambio.join(task))
|
await task.join()
|
||||||
print(await giambio.join(task1))
|
await task1.join()
|
||||||
|
print("All done")
|
||||||
|
|
||||||
loop.start(main)
|
loop.start(main)
|
||||||
|
|
|
@ -30,7 +30,7 @@ class EventLoop:
|
||||||
"""Main event loop for giambio"""
|
"""Main event loop for giambio"""
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if not self.selector.get_map() and not self.to_run:
|
if not self.selector.get_map() and not any((self.to_run + deque(self.paused))):
|
||||||
break
|
break
|
||||||
while self.selector.get_map(): # If there are sockets ready, (re)schedule their associated task
|
while self.selector.get_map(): # If there are sockets ready, (re)schedule their associated task
|
||||||
timeout = 0.0 if self.to_run else None
|
timeout = 0.0 if self.to_run else None
|
||||||
|
@ -70,6 +70,14 @@ class EventLoop:
|
||||||
self.to_run.append(task)
|
self.to_run.append(task)
|
||||||
return task
|
return task
|
||||||
|
|
||||||
|
def schedule(self, coroutine: types.coroutine, when: int):
|
||||||
|
"""Schedules a task for execution after n seconds"""
|
||||||
|
|
||||||
|
self.sequence += 1
|
||||||
|
task = Task(coroutine)
|
||||||
|
heappush(self.paused, (self.clock() + when, self.sequence, task))
|
||||||
|
return task
|
||||||
|
|
||||||
def start(self, coroutine: types.coroutine, *args, **kwargs):
|
def start(self, coroutine: types.coroutine, *args, **kwargs):
|
||||||
self.spawn(coroutine(*args, **kwargs))
|
self.spawn(coroutine(*args, **kwargs))
|
||||||
self.loop()
|
self.loop()
|
||||||
|
|
|
@ -50,4 +50,4 @@ class AsyncSocket(object):
|
||||||
return self.sock.__exit__(*args)
|
return self.sock.__exit__(*args)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"giambio.socket.AsyncSocket({self.sock}, {self.loop})"
|
return f"giambio.socket.AsyncSocket({self.sock}, {self.loop})"
|
||||||
|
|
4
test.py
4
test.py
|
@ -24,7 +24,7 @@ async def make_srv(address: tuple):
|
||||||
conn, addr = await asock.accept()
|
conn, addr = await asock.accept()
|
||||||
logging.info(f"{addr} connected")
|
logging.info(f"{addr} connected")
|
||||||
task = loop.spawn(echo_server(conn, addr))
|
task = loop.spawn(echo_server(conn, addr))
|
||||||
await task.cancel()
|
# await task.cancel() # Cancel task!
|
||||||
|
|
||||||
|
|
||||||
async def echo_server(sock: AsyncSocket, addr: tuple):
|
async def echo_server(sock: AsyncSocket, addr: tuple):
|
||||||
|
@ -43,4 +43,4 @@ async def echo_server(sock: AsyncSocket, addr: tuple):
|
||||||
logging.info(f"Connection from {addr} closed")
|
logging.info(f"Connection from {addr} closed")
|
||||||
|
|
||||||
|
|
||||||
loop.start(make_srv, ('', 1500))
|
loop.start(make_srv, ('', 1501))
|
||||||
|
|
Loading…
Reference in New Issue