Add week 5 materials
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
rabbitmq:
|
||||
image: rabbitmq:3-management
|
||||
hostname: "rabbitmq"
|
||||
ports:
|
||||
- "15672:15672"
|
||||
- "5672:5672"
|
||||
new_task:
|
||||
build:
|
||||
context: new_task
|
||||
worker1:
|
||||
build:
|
||||
context: worker
|
||||
|
||||
worker2:
|
||||
build:
|
||||
context: worker
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM python:3.8
|
||||
|
||||
RUN mkdir /root/app/
|
||||
WORKDIR /root/app/
|
||||
|
||||
RUN pip install pika
|
||||
|
||||
COPY . .
|
||||
|
||||
ENV PYTHONPATH "${PYTHONPATH}:/root/app/"
|
||||
EXPOSE 5000
|
||||
|
||||
CMD ["bash", "-c", "while ! curl -s rabbitmq:15672 > /dev/null; do echo waiting for rabbitmq; sleep 3; done; python -u new_task.py"]
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
import pika
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq', port=5672))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
channel.confirm_delivery()
|
||||
|
||||
for message in ['first message.', 'second message..', 'third message...']:
|
||||
while True:
|
||||
try:
|
||||
channel.basic_publish(
|
||||
exchange='',
|
||||
routing_key='task_queue',
|
||||
body=message,
|
||||
properties=pika.BasicProperties(
|
||||
delivery_mode=2, # make message persistent. Marking messages as persistent doesn't fully guarantee that a message won't be lost. Although it tells RabbitMQ to save the message to disk, there is still a short time window when RabbitMQ has accepted a message and hasn't saved it yet. Also, RabbitMQ doesn't do fsync(2) for every message
|
||||
))
|
||||
except Exception as e:
|
||||
print("retransmit")
|
||||
time.sleep(1)
|
||||
else:
|
||||
break
|
||||
print(" [x] Sent %r" % message)
|
||||
connection.close()
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM python:3.8
|
||||
|
||||
RUN mkdir /root/app/
|
||||
WORKDIR /root/app/
|
||||
|
||||
RUN pip install pika
|
||||
|
||||
COPY . .
|
||||
|
||||
ENV PYTHONPATH "${PYTHONPATH}:/root/app/"
|
||||
EXPOSE 5000
|
||||
|
||||
CMD ["bash", "-c", "while ! curl -s rabbitmq:15672 > /dev/null; do echo waiting for rabbitmq; sleep 3; done; python -u worker.py"]
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python
|
||||
import pika
|
||||
import time
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq', port=5672))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='task_queue', durable=True) # to make sure that the queue will survive a RabbitMQ node restart
|
||||
print(' [*] Waiting for messages.')
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(" [x] Received %r" % body.decode())
|
||||
time.sleep(body.count(b'.'))
|
||||
print(" [x] Done")
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag) # Send ack
|
||||
|
||||
|
||||
channel.basic_qos(prefetch_count=1) # This uses the basic.qos protocol method to tell RabbitMQ not to give more than one message to a worker at a time
|
||||
channel.basic_consume(queue='task_queue', on_message_callback=callback)
|
||||
|
||||
channel.start_consuming()
|
||||
Reference in New Issue
Block a user