-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsender.py
35 lines (28 loc) · 994 Bytes
/
sender.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
import pika
from threading import Thread
class SenderBroker(Thread):
def __init__(self, queue_name):
super().__init__()
self.queue_name = queue_name
def connect(self):
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
self.channel = self.connection.channel()
'''
def direct_connect(self, exchange=None):
self.exchange = exchange
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
self.channel = self.connection.channel()
self.channel.exchange_declare(
exchange=exchange, exchange_type='direct')
'''
def run(self,msg):
self.connect()
self.channel.basic_publish(
exchange='', routing_key=self.queue_name, body=msg)
def send_message(self, msg):
self.run(msg)
def stop(self):
self.join(5)
self.connection.close()