Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
AVPetukhov
Practica 2021
Commits
bc0a7b20
Commit
bc0a7b20
authored
Jul 11, 2021
by
AVPetukhov
Browse files
Upload New File
parent
b56def20
Changes
1
Show whitespace changes
Inline
Side-by-side
consumerV3.py
0 → 100644
View file @
bc0a7b20
# подключение нужных модулей и библиотек
import
json
import
matplotlib.pyplot
as
plt
import
pika
,
sys
,
os
,
math
# подключение нужных списков
x
=
[]
y
=
[]
plt
.
grid
()
def
main
():
# логин и пароль пользователя для входа
credentials
=
pika
.
PlainCredentials
(
'Anton'
,
'Anton'
)
# параметры соединения (IP хоста, порт)
connection
=
pika
.
BlockingConnection
(
pika
.
ConnectionParameters
(
host
=
'217.9.89.214'
,
port
=
5672
,
virtual_host
=
'/'
,
credentials
=
credentials
))
# установка соединения с сервером
channel
=
connection
.
channel
()
# создание очереди
channel
.
queue_declare
(
queue
=
'Anton'
)
# callback функция к очереди, производящая декодировку
# с формата JSON и формирующая список значений x и y
def
callback
(
ch
,
method
,
properties
,
body
):
print
(
"Received %r"
%
body
)
z
=
json
.
loads
(
body
)
print
(
z
)
for
i
in
range
(
151
):
inndict
=
z
[
i
]
x
.
append
(
inndict
[
'x'
])
y
.
append
(
inndict
[
'y'
])
if
len
(
x
)
==
151
and
len
(
y
)
==
151
:
return
graphbuild
(
x
,
y
)
# ф-ия построения графика
def
graphbuild
(
x
,
y
):
plt
.
title
(
'y=x^2'
)
plt
.
plot
(
x
,
y
,
color
=
"#27B685"
)
plt
.
show
()
# указание ,что сообщения из очереди должны поступать в функцию callback
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
:
# метод выхода из Python
sys
.
exit
(
0
)
except
SystemExit
:
# метод выхода из процесса
os
.
_exit
(
0
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment