Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

sockets - With eventlet, callback function invoked only after bg thread exit (python Flask)

I am using Flask and Socketio. Here's a simplification of my code:

from flask import Flask, url_for, render_template, send_from_directory, request, redirect
from flask_socketio import SocketIO, emit, send


app = Flask(__name__)
socketio = SocketIO(app, async_mode='threading')
#socketio = SocketIO(app)  # using eventlet, don't write "async_mode='threading' or it will ignore eventlet


@socketio.on('xxx', namespace='/xxx')
def start_bg(cid):
    # eventlet.spawn(bg_conversion, cid)
    socketio.start_background_task(bg_conversion, cid) # Do some time-consuming things
    print('test')

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

It works fine without eventlet, but now I have the need to stop a thread so I guess eventlet would be good.

However, if I switch to the annotated code, the client side's callback function will only be invoked after the thread(bg_conversion) exits.

I added a print after the spawning line, and it prints immediately - so it is running in the background. But why isn't the callback function on the client's side invoked immediately? Doesn't the function return immediately after the print?

question from:https://stackoverflow.com/questions/65868284/with-eventlet-callback-function-invoked-only-after-bg-thread-exit-python-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...