consumerV3.py 1.19 KB
Newer Older
AVPetukhov's avatar
AVPetukhov committed
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
import json

import matplotlib.pyplot as plt
import pika, sys, os, math

x = []
y = []
plt.grid()

def main():
    credentials = pika.PlainCredentials('Anton', 'Anton')
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='217.9.89.214', port=5672, virtual_host='/', credentials=credentials))
    channel = connection.channel()
    channel.queue_declare(queue='Anton')

    def callback(ch, method, properties, body):
        print("Received %r" % body)
        a = json.loads(body)
        print(a)
        for i in range(151):
            inndict = a[i]
            x.append(inndict['x'])
            y.append(inndict['y'])
        if len(x) == 151 and len(y) == 151:
            return plotbuilder(x,y)

    def plotbuilder(x,y):
        plt.title('y=x^2')
        plt.plot(x,y,color="#27B685")
        plt.show()

    channel.basic_consume(queue='Anton', on_message_callback=callback, auto_ack=True)

    print('Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()


if  __name__  == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)