def start_listen_gnsq(cls, topic, callback): import gnsq consumer = gnsq.Consumer( message_handler=callback, # nsqd_tcp_addresses=['192.168.5.134:4150'], lookupd_http_addresses=['192.168.5.134:4161'], # lookupd_http_addresses=['127.0.0.1:4161'], topic=topic, channel='channel1', max_in_flight=8, ) consumer.start()
def start_listen_gnsq_map(cls, topic, callback): import gevent.pool import gnsq from gnsq.contrib.queue import QueueHandler consumer = gnsq.Consumer( # nsqd_tcp_addresses=['192.168.5.134:4150'], lookupd_http_addresses=['192.168.5.134:4161'], # lookupd_http_addresses=['127.0.0.1:4161'], topic=topic, channel='channel1', max_in_flight=16, ) queue = QueueHandler(maxsize=16) consumer.on_message.connect(queue) consumer.start(block=False) pool = gevent.pool.Pool(16) pool.map(callback, queue)
import gnsq consumer = gnsq.Consumer( 'ABC', 'channel', # nsqd_tcp_addresses='localhost:4150', lookupd_http_addresses='localhost:4161') @consumer.on_message.connect def handler(consumer, message): print(f"Got Message: {message.body.decode('utf-8')}") consumer.start()
from flask import flash, request, render_template, Flask from db import init_db, add_task, get_tasks, task_pending, add_result from work import count_words SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db') NSQ_HTTP_HOST=os.getenv('NSQ_HTTP_HOST', 'localhost') NSQ_HTTP_PORT=os.getenv('NSQ_HTTP_PORT', 4150) NSQ_TOPIC=os.getenv('NSQ_TOPIC', 'counter') app = Flask(__name__) app.logger.setLevel(logging.INFO) app.secret_key = os.getenv('FLASK_SECRET_KEY', 'super secret key') app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False consumer = gnsq.Consumer(NSQ_TOPIC, 'channel', f'{NSQ_HTTP_HOST}:{NSQ_HTTP_PORT}') @app.route('/tasks', methods = ['GET', 'POST']) def tasks(): if request.method == 'POST': if not request.form['address']: flash('Enter site:') else: err, new_task = add_task(request.form['address']) if err: flash(str(err)) else: task_dict = { 'id': new_task.id, 'address': new_task.address
import gnsq import json import time import threading import requests from random import randint from stem.control import Controller import os import socket NSQ_HOST = "108.61.250.38:4150" NSQ_TOPIC = "task12" NSQ_CONSUMER_CHANNEL = socket.gethostname() consumer = gnsq.Consumer(NSQ_TOPIC, NSQ_CONSUMER_CHANNEL, NSQ_HOST, max_tries=1) FINGERPRINT = [ "85359D7950AC7906E66A2E7D4515C90A2DB73CA7", "91F5F66C82F93546C1E8D1C5D30BE9B446F7A1D0" ] @consumer.on_message.connect def handler(consumer, message): ''' accept the command that build a node or delete it. :param consumer: :param message: :return: '''