-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
event.py: "nicegui_globals.loop.is_running" raises Attribute Error: 'NoneType' object has no attribute 'is_running' #18
Comments
It could be that your mqtt is not running on the same event loop. RoSys, NiceGUI and the underlying FastAPI are async frameworks. That means, they run an event loop and no io-bound or cpu-bound tasks should be directly executed on the main thread but rather be "awaited". See https://fastapi.tiangolo.com/async/ for a good in-depth explanation. You might want to try asyncio-mqtt, aiomqtt or gmqtt. |
Thanks for the fast response! On the README.md of I tried it out and it works as expected. Here is an example. Here the complete example: import json
import uuid
import gmqtt.mqtt
import numpy as np
import rosys
from fastapi_mqtt import FastMQTT, MQTTConfig
from nicegui.globals import app
class MqttListener:
def __init__(
self,
ip_address:str,
port: int,
topic: str,
) -> None:
self.UPDATED = rosys.event.Event()
self._ip_address = ip_address
self._port = port
self._topic = topic
mqtt_config = MQTTConfig(host=ip_address, port=port, keepalive=60)
mqtt = FastMQTT(
config=mqtt_config,
client_id=self.__class__.__name__
)
self.mqtt = mqtt
mqtt.init_app(app) #use the same fastapi app as the nicegui app
@mqtt.on_connect()
def connect(client: gmqtt.client.Client, flags, rc, properties):
mqtt.client.subscribe(self._topic)
print(f"{self.__class__.__name__} connected to mqtt ")
@mqtt.on_message()
async def message(client: gmqtt.client.Client, topic, payload, qos, properties):
if topic == self._topic:
self.UPDATED.emit(payload.decode("utf-8"))
@mqtt.on_disconnect()
def disconnect(client: gmqtt.client.Client, packet, exc=None):
print(f"{self.__class__.__name__} disconnected from mqtt")
|
Looks great! I'll close the issue then. |
Hi,
I receive an error, that makes no sense for me for several reasons.
My code is the following:
Whenever the Event is emitted via
self.UPDATED.emit(velocity)
, I get the following error:The handle_velocity function is invoked, so everything works for me, but the error log messages bombards the console.
I looked into the event.py:
we can see, that the condition, that raises the Attribute Error is only executed, if the result is an instance of Awaitable.
But my
handle_velocity
function does not return an Awaitable.So, I have two questions:
nicegui_globals.loop
a 'NoneType' object? - and how can I change this?if isinstance(result, Awaitable):
true although I do not return anything inhandle_velocity
?pip show
return 0.9.0 for rosys and 1.3.2 for niceguiIt would be very nice, if you could help me, because I really don't understand the reason for the error.
Best regards
Andreas
The text was updated successfully, but these errors were encountered: