示例#1
0
def main():
    tasks = [
        Producer(),
        Consumer()
    ]

    for t in tasks:
        t.start()

    time.sleep(10)

    for task in tasks:
        task.stop()

    for task in tasks:
        task.join()
示例#2
0
import os
from json import dumps

from flask import Flask, abort, request

from kafka.producer import Producer
from kafka.consumer import Consumer
from elastic.elasticsearch import Elastic

PRODUCER = Producer()
CONSUMER = Consumer()
ELASTIC_SEARCH = Elastic()
CONTENT_TYPE_TEXT = 'text/plain'
DEBUG = os.getenv('DEBUG', False)
app = Flask(__name__)


@app.route('/health')
def hello():
    return 'Strong like a bull!'


@app.route('/message', methods=['POST'])
def post_message():
    if request.content_type != CONTENT_TYPE_TEXT:
        abort(400)

    PRODUCER.send(request.data.decode('utf-8'))
    messages = CONSUMER.get()
    if not messages:
        abort(500)