简体   繁体   中英

How to stop/kill a thread python on Heroku?

Locally, on windows, i can stop my thread raising an exception :

def raise_exception_in_thread(t:Thread):
    ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(t.ident), ctypes.py_object(SystemExit))

But, on Heroku, my thread don't stop.

I tried :

ctypes.pythonapi.PyThreadState_SetAsyncExc(t.native_id, ctypes.py_object(SystemExit))
ctypes.pythonapi.PyThreadState_SetAsyncExc(t.ident, ctypes.py_object(SystemExit))
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_ulong(t.native_id), ctypes.py_object(SystemExit))
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_ulong(t.ident), ctypes.py_object(SystemExit))
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(t.native_id), ctypes.py_object(SystemExit))

I found a solution if it could help :

I deployed a Flask/SocketIO app on heroku using async_mode 'gevent' :

app = Flask(__name__)
socketio = SocketIO(app, async_mode='gevent')

Procfile:

web: gunicorn -k gevent -w 1 main:app

And to be able to stop a thread on heroku by raising an exception,
i have to deploy the Flask/SocketIO app on heroku using async_mode 'threading' :

app = Flask(__name__)
socketio = SocketIO(app, async_mode='threading')

Procfile:

web: gunicorn -w 1 --threads 100 main:app

https://flask-socketio.readthedocs.io/en/latest/api.html
https://flask-socketio.readthedocs.io/en/latest/deployment.html

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