You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ python3.7 ./mqtt.py
starting thread ...
starting server ...
Task was destroyed but it is pending!
task: <Task pending coro=<Broker._broadcast_loop() running at venv/lib/python3.7/site-packages/hbmqtt/broker.py:696> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x8027fd890>()]>>
Exception ignored in: <generator object Broker._broadcast_loop at 0x8027d1ed0>
Traceback (most recent call last):
File "venv/lib/python3.7/site-packages/hbmqtt/broker.py", line 696, in _broadcast_loop
File "/usr/local/lib/python3.7/asyncio/queues.py", line 161, in get
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 687, in call_soon
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 479, in _check_closed
RuntimeError: Event loop is closed
But running the coroutine test_coroutine() instead just works as intended. If asyncio.set_event_loop(self.loop) is added the thread starts but immediately finishes:
def start(self):
print('starting thread ...')
self.loop = asyncio.new_event_loop()
# Setting the event loop:
asyncio.set_event_loop(self.loop)
print('starting server ...')
# [...]
@interkosmos mixing threads with asyncio is generally not recommended.
If you need to (for example you have a not async written code or something cpu heavy) have a look at ProcessPoolExecutor or ThreadPoolExecutor. They allow you to run code in parallel to async code but keep the async loop in control.
How can I run an HBMQTT broker instance inside a Python thread? The following minimal example doesn’t work:
This basic example crashes:
But running the coroutine
test_coroutine()
instead just works as intended. Ifasyncio.set_event_loop(self.loop)
is added the thread starts but immediately finishes:Output:
The text was updated successfully, but these errors were encountered: