Added file for experiments with the framework

This commit is contained in:
nocturn9x 2020-03-20 18:06:12 +01:00
parent bf3c477b02
commit 25c404c961
4 changed files with 60 additions and 29 deletions

30
experiment.py Normal file
View File

@ -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)

View File

@ -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"]

View File

@ -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,

View File

@ -1,9 +1,9 @@
import giambio
from giambio.core import AsyncSocket
from giambio.socket import AsyncSocket
import socket
import logging
loop = giambio.EventLoop()
loop = giambio.core.EventLoop()
logging.basicConfig(level=20,
format="[%(levelname)s] %(asctime)s %(message)s",