mirror of https://github.com/nocturn9x/giambio.git
Added file for experiments with the framework
This commit is contained in:
parent
bf3c477b02
commit
25c404c961
|
@ -0,0 +1,30 @@
|
|||
import giambio
|
||||
|
||||
|
||||
loop = giambio.EventLoop()
|
||||
|
||||
|
||||
async def countdown(n):
|
||||
while n > 0:
|
||||
print(f"Down {n}")
|
||||
n -= 1
|
||||
await giambio.sleep(1)
|
||||
return "Count DOWN over"
|
||||
|
||||
|
||||
async def count(stop, step=1):
|
||||
x = 0
|
||||
while x < stop:
|
||||
print(f"Up {x}")
|
||||
x += step
|
||||
await giambio.sleep(step)
|
||||
return "Count UP over"
|
||||
|
||||
|
||||
async def main():
|
||||
task = loop.spawn(countdown(8))
|
||||
task1 = loop.spawn(count(8, 2))
|
||||
print(await giambio.join(task))
|
||||
print(await giambio.join(task1))
|
||||
|
||||
loop.start(main)
|
|
@ -1,4 +1,5 @@
|
|||
__author__ = "Nocturn9x aka Isgiambyy"
|
||||
__version__ = (0, 0, 1)
|
||||
from .core import EventLoop, join, sleep
|
||||
__all__ = ["EventLoop", "join", "sleep"]
|
||||
|
||||
__all__ = ["EventLoop", "join", "sleep"]
|
|
@ -7,34 +7,8 @@ from .exceptions import AlreadyJoinedError, CancelledError
|
|||
import traceback
|
||||
from timeit import default_timer
|
||||
from time import sleep as wait
|
||||
from .socket import AsyncSocket
|
||||
|
||||
|
||||
class Task:
|
||||
|
||||
"""A simple wrapper around a coroutine object"""
|
||||
|
||||
def __init__(self, coroutine: types.coroutine):
|
||||
self.coroutine = coroutine
|
||||
self.status = False # Not ran yet
|
||||
self.joined = False
|
||||
self.ret_val = None # Return value is saved here
|
||||
self.exception = None # If errored, the exception is saved here
|
||||
self.cancelled = False # When cancelled, this is True
|
||||
|
||||
def run(self):
|
||||
self.status = True
|
||||
return self.coroutine.send(None)
|
||||
|
||||
def __repr__(self):
|
||||
return f"giambio.core.Task({self.coroutine}, {self.status}, {self.joined}, {self.ret_val}, {self.exception}, {self.cancelled})"
|
||||
|
||||
async def cancel(self):
|
||||
return await cancel(self)
|
||||
|
||||
async def join(self):
|
||||
return await join(self)
|
||||
|
||||
|
||||
class EventLoop:
|
||||
|
||||
|
@ -149,6 +123,32 @@ class EventLoop:
|
|||
return sock.connect(addr)
|
||||
|
||||
|
||||
class Task:
|
||||
|
||||
"""A simple wrapper around a coroutine object"""
|
||||
|
||||
def __init__(self, coroutine: types.coroutine):
|
||||
self.coroutine = coroutine
|
||||
self.status = False # Not ran yet
|
||||
self.joined = False
|
||||
self.ret_val = None # Return value is saved here
|
||||
self.exception = None # If errored, the exception is saved here
|
||||
self.cancelled = False # When cancelled, this is True
|
||||
|
||||
def run(self):
|
||||
self.status = True
|
||||
return self.coroutine.send(None)
|
||||
|
||||
def __repr__(self):
|
||||
return f"giambio.core.Task({self.coroutine}, {self.status}, {self.joined}, {self.ret_val}, {self.exception}, {self.cancelled})"
|
||||
|
||||
async def cancel(self):
|
||||
return await cancel(self)
|
||||
|
||||
async def join(self):
|
||||
return await join(self)
|
||||
|
||||
|
||||
@types.coroutine
|
||||
def sleep(seconds: int):
|
||||
"""Pause the execution of a coroutine for the passed amount of seconds,
|
||||
|
|
Loading…
Reference in New Issue