简体   繁体   中英

Weird behaviour using FLASK socketIO in python with the library eventlet

i'm trying here to interact with my back end (Python 2.7 FLASK) by pressing buttons on my front end HTML.

Here the behaviour I expect :

My state machine is in wait Status (waiting for user action)

startEvent.wait()

When I press the button START, I set an event in my back end

@GUI.route('/ptu1', methods=['GET', 'POST'])
def startButton():
    if request.method == 'POST':
        startEvent.set()
        return redirect(url_for('index'))

then I send a message on my html page with a socketIO like this

    socket_gui.emit('socket_field',
                     {'data': msg, 'count': self.count,'flag': 1},
                     namespace='/test')

The javascript code which receive this message is:

socket.on('socket_field', function(msg) {

            $('#Status').text(msg.data);

            if (msg.flag == '1'){

                //do things
            }
            if (msg.flag == '2'){

                //do others things
            }

        });

For now, this code works because when I press my start button, it send the event and then send the socket to my front end javascript which do the action I want.

Then I go in wait mode (waiting for the user to push the second button)

printEvent.wait()
socket_gui.emit('socket_field',
                     {'data': msg, 'count': self.count,'flag': 2},
                     namespace='/test')

I set the event when I click the button:

@GUI.route('/print1', methods=['GET', 'POST'])
def printButton():
    if request.method == 'POST':
        printEvent.set()
        return redirect(url_for('index'))

AND HERE IS THE PROBLEM : The first step with the start button works perfectly, the socket is emited and the javascript receive it and treat it as i want.

But when I press the Print button, I can see in my logs that the socket is emited correctly with the good args but the javascript in front end never receive it.

It seems that the wait event block the emit socket.

In fact, I tried to send a message with the socket before the wait and it 's working.

I think the problem is in the library I use : evenlet I tried with a gevent library and it worked but it's not that fast. I'd like to use the eventlet library but I don't know how to fix my problem.

Alright, I found the solution :

In fact, the sleep from time library in python was blocking my socket throught the GUI. To solve this problem, I had to use the sleep function from the eventlet library like this :

import sleep from eventlet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM