-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
36 lines (25 loc) · 1002 Bytes
/
server.py
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
#!/usr/bin/env python
import pika
from g2w_models.predictor import Predictor
from g2w_rabbitmq.config import RPC_QUEUE
def on_request(ch, method, props, body):
message = body.decode("utf-8")
print(" [x] Received {}".format(message))
response = clf.predict(message)
ch.basic_publish(exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(correlation_id=props.correlation_id),
body=str(response))
ch.basic_ack(delivery_tag=method.delivery_tag)
def main():
global clf
clf = Predictor()
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue=RPC_QUEUE, durable=True, auto_delete=True)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_request, queue=RPC_QUEUE)
print(" [x] Awaiting RPC requests")
channel.start_consuming()
if __name__ == '__main__':
main()