File: loop_select.py

package info (click to toggle)
python-paho-mqtt 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,248 kB
  • sloc: python: 8,765; sh: 48; makefile: 40
file content (90 lines) | stat: -rwxr-xr-x 2,576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3

import socket
import uuid
from select import select
from time import time

import paho.mqtt.client as mqtt

client_id = 'paho-mqtt-python/issue72/' + str(uuid.uuid4())
topic = client_id
print("Using client_id / topic: " + client_id)


class SelectMqttExample:
    def __init__(self):
        pass

    def on_connect(self, client, userdata, flags, reason_code, properties):
        print("Subscribing")
        client.subscribe(topic)

    def on_message(self, client, userdata, msg):
        if self.state not in {1, 3, 5}:
            print("Got unexpected message: {}".format(msg.decode()))
            return

        print("Got message with len {}".format(len(msg.payload)))
        self.state += 1
        self.t = time()

    def on_disconnect(self, client, userdata, flags, reason_code, properties):
        self.disconnected = True, reason_code

    def do_select(self):
        sock = self.client.socket()
        if not sock:
            raise Exception("Socket is gone")

        print("Selecting for reading" + (" and writing" if self.client.want_write() else ""))
        r, w, e = select(
            [sock],
            [sock] if self.client.want_write() else [],
            [],
            1
        )

        if sock in r:
            print("Socket is readable, calling loop_read")
            self.client.loop_read()

        if sock in w:
            print("Socket is writable, calling loop_write")
            self.client.loop_write()

        self.client.loop_misc()

    def main(self):
        self.disconnected = (False, None)
        self.t = time()
        self.state = 0

        self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=client_id)
        self.client.on_connect = self.on_connect
        self.client.on_message = self.on_message
        self.client.on_disconnect = self.on_disconnect

        self.client.connect('mqtt.eclipseprojects.io', 1883, 60)
        print("Socket opened")
        self.client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)

        while not self.disconnected[0]:
            self.do_select()

            if self.state in {0, 2, 4}:
                if time() - self.t >= 5:
                    print("Publishing")
                    self.client.publish(topic, b'Hello' * 40000)
                    self.state += 1

            if self.state == 6:
                self.state += 1
                self.client.disconnect()

        print("Disconnected: {}".format(self.disconnected[1]))


print("Starting")
SelectMqttExample().main()
print("Finished")