PyCUDA context error when using Flask
Let me present one solution here because I have tried a lot of solutions but still not work. Fortunately I found one correct answer.
some solutions like
import pycuda.autoinit
or
cuda.init device = cuda.Device(0) ctx = device.make_context() inputs, outputs, bindings, stream = allocate_buffer() ctx.pop()
these may work if you run the script as simple program, but it will raise context error if you run with flask or other web servers. According to my searching, the reason is possibally that flask server would spawn new thread when a request came in.
The real solution in such a circumstance is quite simple and you should just add code like this:
with engine.create_execution_context() as context: ctx = cuda.Context.attach() inputs, outputs, bindings, stream = allocate_buffer() ctx.detach()
This works for me
Starting the Flask application in non-threaded mode worked for me
app.run(host=HOST, port=PORT, debug=False,threaded=False)