I tried to send message and receive message using RabbitMQ. I dont have computer science background, the terms I used could not be very accurate.
I try to copy the tutorial file: When submitting my html form, my python script (cgi) the message is submitting to the queue
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = PN
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
connection.close()
my receiver is running :
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] Received Project %r" % body)
#ch.basic_ack(delivery_tag = method.delivery_tag)
if not (os.path.isfile(js_path)):
print (' [*] ERROR files missing ')
#ch.basic_ack(delivery_tag = method.delivery_tag)
return
p= subprocess.Popen(run a subprocess here)
p.wait()
print (' [*] Temporary Files removed')
print(" [*] Waiting for messages. To exit press CTRL+C")
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,queue='task_queue',no_ack=True)
channel.start_consuming()
It manages most of the time but randomly crash with the following error:
Traceback (most recent call last):
File "Receive5.py", line 139, in <module>
channel.start_consuming()
File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 1681, in start_consuming
self.connection.process_data_events(time_limit=None)
File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 647, in process_data_events
self._flush_output(common_terminator)
File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 426, in _flush_output
raise exceptions.ConnectionClosed() pika.exceptions.ConnectionClosed