87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
"""
|
|
aiosched: I'm bored and I'm making an async event loop again
|
|
|
|
Copyright (C) 2020 nocturn9x
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https:www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
"""
|
|
|
|
import inspect
|
|
from threading import local
|
|
from timeit import default_timer
|
|
from aiosched.kernel import FIFOKernel
|
|
from aiosched.errors import SchedulerError
|
|
from aiosched.util.debugging import BaseDebugger
|
|
from typing import Coroutine, Callable, Any
|
|
|
|
|
|
local_storage = local()
|
|
|
|
|
|
def get_event_loop():
|
|
"""
|
|
Returns the event loop associated to the current
|
|
thread
|
|
"""
|
|
|
|
try:
|
|
return local_storage.loop
|
|
except AttributeError:
|
|
raise SchedulerError("loop is not running") from None
|
|
|
|
|
|
def new_event_loop(clock_function: Callable, debugger: BaseDebugger | None = None):
|
|
"""
|
|
Associates a new event loop to the current thread
|
|
and deactivates the old one. This should not be
|
|
called explicitly unless you know what you're doing.
|
|
If an event loop is currently set, and it is running,
|
|
a SchedulerError exception is raised
|
|
"""
|
|
|
|
try:
|
|
loop = get_event_loop()
|
|
except SchedulerError:
|
|
local_storage.loop = FIFOKernel(clock_function, debugger)
|
|
else:
|
|
if not loop.done():
|
|
raise SchedulerError("cannot change event loop while running")
|
|
else:
|
|
loop.close()
|
|
local_storage.loop = FIFOKernel(clock_function, debugger)
|
|
|
|
|
|
def run(func: Callable[[Any, Any], Coroutine[Any, Any, Any]], debugger: BaseDebugger | None = None, *args, **kwargs):
|
|
"""
|
|
Starts the event loop from a synchronous entry point
|
|
"""
|
|
|
|
if inspect.iscoroutine(func):
|
|
raise SchedulerError(
|
|
"Looks like you tried to call aiosched.run(your_func(arg1, arg2, ...)), that is wrong!"
|
|
"\nWhat you wanna do, instead, is this: aiosched.run(your_func, arg1, arg2, ...)"
|
|
)
|
|
elif not inspect.iscoroutinefunction(func):
|
|
raise SchedulerError("aiosched.run() requires an async function as parameter!")
|
|
new_event_loop(kwargs.get("clock", default_timer), debugger)
|
|
get_event_loop().start(func, *args)
|
|
|
|
|
|
def clock() -> float:
|
|
"""
|
|
Returns the current clock time of the thread-local event
|
|
loop
|
|
"""
|
|
|
|
return get_event_loop().clock()
|