Issue
I'm studying the HBMQTT docs at hbmqtt and there are 2 functions which do the same thing, basically to publish 3 messages to an MQTT broker:
@asyncio.coroutine
def test_coro():
C = MQTTClient()
yield from C.connect('mqtt://test.mosquitto.org/')
tasks = [
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_0')),
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)),
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=QOS_2)),
]
yield from asyncio.wait(tasks)
logger.info("messages published")
yield from C.disconnect()
@asyncio.coroutine
def test_coro2():
try:
C = MQTTClient()
ret = yield from C.connect('mqtt://test.mosquitto.org:1883/')
message = yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_0', qos=QOS_0)
message = yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)
message = yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=QOS_2)
#print(message)
logger.info("messages published")
yield from C.disconnect()
except ConnectException as ce:
logger.error("Connection failed: %s" % ce)
asyncio.get_event_loop().stop()
The docs go on to describe them as:
test_coro() publish 3 messages in sequence. test_coro2() publishes the same message asynchronously.
This makes no sense since I've also been studying asyncio
and as I understand it ensure_future()
schedules tasks to run asynchronously whereas yield from
seems to serve the same purpose as await
and thus each of those statements will simply block till the task is completed. So the description should be the other way round. Am I right or missing something?
Solution
Yes, the names of the coroutines are swapped and should be the other way round.
Furthermore: the code is outdated. i.e.
There is no need to mark the functions as coroutines with such decorator anymore,, just use the
async
keyword.use
await
instead ofyield from
Answered By - Pynchia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.