-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contexts async multiple chats
- Loading branch information
Showing
28 changed files
with
689 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
**/__pycache__/ | ||
|
||
|
||
|
||
# Ignore all contents of the virtual environment directory | ||
.venv/* | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import asyncio | ||
import threading | ||
from concurrent.futures import Future | ||
|
||
class DeferredTask: | ||
def __init__(self, func, *args, **kwargs): | ||
self._loop = asyncio.new_event_loop() | ||
# self._thread = None | ||
self._task = None | ||
self._future = Future() | ||
self._start_task(func, *args, **kwargs) | ||
|
||
def _start_task(self, func, *args, **kwargs): | ||
def run_in_thread(loop, func, args, kwargs): | ||
asyncio.set_event_loop(loop) | ||
self._task = loop.create_task(self._run(func, *args, **kwargs)) | ||
loop.run_forever() | ||
|
||
self._thread = threading.Thread(target=run_in_thread, args=(self._loop, func, args, kwargs)) | ||
self._thread.start() | ||
|
||
async def _run(self, func, *args, **kwargs): | ||
try: | ||
result = await func(*args, **kwargs) | ||
self._future.set_result(result) | ||
except Exception as e: | ||
self._future.set_exception(e) | ||
finally: | ||
self._loop.call_soon_threadsafe(self._loop.stop) | ||
|
||
def is_ready(self): | ||
return self._future.done() | ||
|
||
async def result(self, timeout=None): | ||
if self._task is None: | ||
raise RuntimeError("Task was not initialized properly.") | ||
|
||
try: | ||
return await asyncio.wait_for(asyncio.wrap_future(self._future), timeout) | ||
except asyncio.TimeoutError: | ||
raise TimeoutError("The task did not complete within the specified timeout.") | ||
|
||
def result_sync(self, timeout=None): | ||
try: | ||
return self._future.result(timeout) | ||
except TimeoutError: | ||
raise TimeoutError("The task did not complete within the specified timeout.") | ||
|
||
def kill(self): | ||
if self._task and not self._task.done(): | ||
self._loop.call_soon_threadsafe(self._task.cancel) | ||
|
||
def is_alive(self): | ||
return self._thread.is_alive() and not self._future.done() | ||
|
||
def __del__(self): | ||
if self._loop.is_running(): | ||
self._loop.call_soon_threadsafe(self._loop.stop) | ||
if self._thread.is_alive(): | ||
self._thread.join() | ||
self._loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,78 @@ | ||
from dataclasses import dataclass | ||
from dataclasses import dataclass, field | ||
import json | ||
from typing import Optional, Dict | ||
import uuid | ||
|
||
|
||
@dataclass | ||
class LogItem: | ||
log: 'Log' | ||
no: int | ||
type: str | ||
heading: str | ||
content: str | ||
kvps: Optional[Dict] = None | ||
|
||
guid: str = "" | ||
|
||
def __post_init__(self): | ||
self.guid = self.log.guid | ||
|
||
def update(self, type: str | None = None, heading: str | None = None, content: str | None = None, kvps: dict | None = None): | ||
if self.guid == self.log.guid: | ||
self.log.update_item(self.no, type=type, heading=heading, content=content, kvps=kvps) | ||
|
||
def output(self): | ||
return { | ||
"no": self.no, | ||
"type": self.type, | ||
"heading": self.heading, | ||
"content": self.content, | ||
"kvps": self.kvps | ||
} | ||
|
||
class Log: | ||
|
||
guid = uuid.uuid4() | ||
version: int = 0 | ||
last_updated: int = 0 | ||
logs: list = [] | ||
|
||
def __init__(self, type: str="placeholder", heading: str="", content: str="", kvps: dict|None = None): | ||
self.item = Log.log(type, heading, content, kvps) # create placeholder log item that will be updated | ||
|
||
def update(self, type: Optional[str] = None, heading: str|None = None, content: str|None = None, kvps: dict|None = None): | ||
Log.edit(self.item.no, type=type, heading=heading, content=content, kvps=kvps) | ||
|
||
@staticmethod | ||
def reset(): | ||
Log.guid = uuid.uuid4() | ||
Log.version = 0 | ||
Log.last_updated = 0 | ||
Log.logs = [] | ||
|
||
@staticmethod | ||
def log(type: str, heading: str|None = None, content: str|None = None, kvps: dict|None = None): | ||
item = LogItem(len(Log.logs), type, heading or "", content or "", kvps) | ||
Log.logs.append(item) | ||
Log.last_updated = item.no | ||
Log.version += 1 | ||
def __init__(self): | ||
self.guid: str = str(uuid.uuid4()) | ||
self.updates: list[int] = [] | ||
self.logs: list[LogItem] = [] | ||
|
||
def log(self, type: str, heading: str | None = None, content: str | None = None, kvps: dict | None = None) -> LogItem: | ||
item = LogItem(log=self,no=len(self.logs), type=type, heading=heading or "", content=content or "", kvps=kvps) | ||
self.logs.append(item) | ||
self.updates += [item.no] | ||
return item | ||
|
||
@staticmethod | ||
def edit(no: int, type: Optional[str] = None, heading: str|None = None, content: str|None = None, kvps: dict|None = None): | ||
if 0 <= no < len(Log.logs): | ||
item = Log.logs[no] | ||
if type is not None: | ||
item.type = type | ||
if heading is not None: | ||
item.heading = heading | ||
if content is not None: | ||
item.content = content | ||
if kvps is not None: | ||
item.kvps = kvps | ||
|
||
Log.last_updated = no | ||
Log.version += 1 | ||
else: | ||
raise IndexError("Log item number out of range") | ||
|
||
def update_item(self, no: int, type: str | None = None, heading: str | None = None, content: str | None = None, kvps: dict | None = None): | ||
item = self.logs[no] | ||
if type is not None: | ||
item.type = type | ||
if heading is not None: | ||
item.heading = heading | ||
if content is not None: | ||
item.content = content | ||
if kvps is not None: | ||
item.kvps = kvps | ||
self.updates += [item.no] | ||
|
||
def output(self, start=None, end=None): | ||
if start is None: | ||
start = 0 | ||
if end is None: | ||
end = len(self.updates) | ||
|
||
out = [] | ||
seen = set() | ||
for update in self.updates[start:end]: | ||
if update not in seen: | ||
out.append(self.logs[update].output()) | ||
seen.add(update) | ||
|
||
return out | ||
|
||
|
||
|
||
def reset(self): | ||
self.guid = str(uuid.uuid4()) | ||
self.updates = [] | ||
self.logs = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.