-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
71 lines (60 loc) · 1.87 KB
/
console.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
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
#!/usr/bin/env python
##
## Client that accepts messages encoded as integers
## computes the fibonacci of those numbers and returns
## a string representation
##
import pika
import threading,time,sys
import fib_pb2
QHost = os.getenv('QHOST')
connection = pika.BlockingConnection(
pika.ConnectionParameters(host=QHost))
recvchan = connection.channel()
sendchan = connection.channel()
recvchan.queue_declare(queue='fib_to_compute', durable=True)
recvchan.queue_declare(queue='fib_from_compute', durable=True)
def receive_fib(ch, method, properties, body):
ch.basic_ack(delivery_tag = method.delivery_tag)
try:
fiblist = fib_pb2.FibList()
fiblist.ParseFromString(body)
for fibInstance in fiblist.fibs:
print "Fib(",fibInstance.n,") = ", fibInstance.response
finally:
pass
class RecvThread(threading.Thread):
def run(self):
recvchan.basic_qos(prefetch_count=1)
recvchan.basic_consume(receive_fib, queue='fib_from_compute', no_ack=False)
recvchan.start_consuming()
recvThread = RecvThread()
recvThread.daemon = True
recvThread.start()
while True:
try:
n = raw_input("Fib of what number (0 to exit)? > ")
n = int(n)
except ValueError:
print "Enter an integer"
n = 0
except EOFError:
n = 0
if n >= 100000:
print "What are you trying to crash this or something? Less than 100000 please"
continue
if n == 0 :
print "Done, exiting..."
break
fiblist = fib_pb2.FibList()
fib = fiblist.fibs.add()
try:
fib.n = n
except TypeError:
fib.n = 0
sendchan.basic_publish(exchange='',
routing_key='fib_to_compute',
body=fiblist.SerializeToString(),
properties=pika.BasicProperties(delivery_mode = 2))
print "Sent.."
sys.exit(0)