Передача данных из Python Queue () в колбу с помощью socketIO - PullRequest
0 голосов
/ 05 октября 2019

Я пытаюсь передать элемент из объекта Queue в колбу с помощью socketIO. Я получаю "not" в качестве вывода в console.log, тогда как в терминале я получаю элемент Queue в качестве вывода. SocketIo не может передать элемент очереди клиенту,Это всего лишь тестовый код. Я использую код создателя набора сетевых данных из github. Вот ссылка. https://github.com/nrajasin/Network-intrusion-dataset-creator/blob/master/counts.py В строке кода 269 я добавил web.put(set.Dataset), как показано ниже, но проблема в том, что socket.emit не можетдать ответ клиенту.

counts.py

from queue import *
global web
web = Queue()
Data = {"item1":"apple","item2":"mango"}
web.put(Data)

app.py

import counts
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from time import sleep
from threading import Thread, Event
import set

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

socketio = SocketIO(app)

thread = Thread()
thread_stop_event = Event()

class Random(Thread):
    def __init__(self):
        self.delay = 1
        super(RandomThread, self).__init__()

    def randomGenerator(self):
        while not thread_stop_event.isSet():
            if counts.web.empty()==False:
                Data = counts.web.get()
                print(Data)
            else:
                Data = "not"
                print(Data)
            socketio.emit('newnumber', {'number': Data}, namespace='/test')
            sleep(self.delay)

    def run(self):
        self.randomGenerator()

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('connect', namespace='/test')
def test_connect():
    # need visibility of the global thread object
    global thread
    print('Client connected')

    if not thread.isAlive():
        print("Starting Thread")
        thread = RandomThread()
        thread.start()

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')


if __name__ == '__main__':
    socketio.run(app)


Терминальный выход

{'item1': 'apple', 'item2': 'mango'}
not
not
not
not
not
not
{'item1': 'apple', 'item2': 'mango'}
not
not
{'item1': 'apple', 'item2': 'mango'}

console.log () выход

not
not
not
...