Added utility get_entities_engine() function.

This commit is contained in:
Fabio Manganiello 2023-03-10 11:49:23 +01:00
parent 60da930e4b
commit 8ccf3e804d
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,4 +1,6 @@
from datetime import datetime, timedelta
import logging
from threading import Event
from typing import Collection, Optional
from ._base import (
@ -36,6 +38,23 @@ def init_entities_engine() -> EntitiesEngine:
return _engine
def get_entities_engine(timeout: Optional[float] = None) -> EntitiesEngine:
"""
Return the running entities engine.
:param timeout: Timeout in seconds (default: None).
"""
time_start = datetime.utcnow()
while not timeout or (datetime.utcnow() - time_start < timedelta(seconds=timeout)):
if _engine:
break
Event().wait(1)
assert _engine, 'The entities engine has not been initialized'
return _engine
def publish_entities(
entities: Collection[Entity], callback: Optional[EntitySavedCallback] = None
) -> None: