mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 16:50:02 +02:00
adds a context manager to the client, fixes running loop issue
This commit is contained in:
parent
9aad9dfbc6
commit
82d6a7f895
@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
import threading
|
import threading
|
||||||
from typing import TypedDict, cast
|
from typing import TypedDict, cast
|
||||||
|
|
||||||
@ -10,6 +11,12 @@ from pydase.client.proxy_loader import ProxyClassMixin, ProxyLoader
|
|||||||
from pydase.utils.serialization.deserializer import loads
|
from pydase.utils.serialization.deserializer import loads
|
||||||
from pydase.utils.serialization.types import SerializedDataService, SerializedObject
|
from pydase.utils.serialization.types import SerializedDataService, SerializedObject
|
||||||
|
|
||||||
|
if sys.version_info < (3, 11):
|
||||||
|
from typing_extensions import Self
|
||||||
|
else:
|
||||||
|
from typing import Self
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -24,7 +31,10 @@ class NotifyDict(TypedDict):
|
|||||||
|
|
||||||
def asyncio_loop_thread(loop: asyncio.AbstractEventLoop) -> None:
|
def asyncio_loop_thread(loop: asyncio.AbstractEventLoop) -> None:
|
||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
|
try:
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
except RuntimeError:
|
||||||
|
logger.debug("Tried starting even loop, but it is running already")
|
||||||
|
|
||||||
|
|
||||||
class ProxyClass(ProxyClassMixin, pydase.components.DeviceConnection):
|
class ProxyClass(ProxyClassMixin, pydase.components.DeviceConnection):
|
||||||
@ -108,6 +118,13 @@ class Client:
|
|||||||
self._thread.start()
|
self._thread.start()
|
||||||
self.connect(block_until_connected=block_until_connected)
|
self.connect(block_until_connected=block_until_connected)
|
||||||
|
|
||||||
|
def __enter__(self) -> Self:
|
||||||
|
self.connect(block_until_connected=True)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __del__(self) -> None:
|
||||||
|
self.disconnect()
|
||||||
|
|
||||||
def connect(self, block_until_connected: bool = True) -> None:
|
def connect(self, block_until_connected: bool = True) -> None:
|
||||||
connection_future = asyncio.run_coroutine_threadsafe(
|
connection_future = asyncio.run_coroutine_threadsafe(
|
||||||
self._connect(), self._loop
|
self._connect(), self._loop
|
||||||
|
Loading…
x
Reference in New Issue
Block a user