Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Flask时出现PyCUDA上下文错误_Python_Image Processing_Flask_Server_Pycuda - Fatal编程技术网

Python 使用Flask时出现PyCUDA上下文错误

Python 使用Flask时出现PyCUDA上下文错误,python,image-processing,flask,server,pycuda,Python,Image Processing,Flask,Server,Pycuda,我正在使用PyCUDA实现平滑的局部仿射,如图所示。当我在linux上运行这个程序时,它工作得很好。但当我试图在Flask上下文下导入它时: from smooth_local_affine import smooth_local_affine from flask import Flask app = Flask(_name_) ... 出现以下错误: 然后我尝试添加context.pop(),然后出现另一个错误 atexit中出错。\u运行\u退出功能:回溯(最近一次调用最后一次): 文件

我正在使用PyCUDA实现平滑的局部仿射,如图所示。当我在linux上运行这个程序时,它工作得很好。但当我试图在Flask上下文下导入它时:

from smooth_local_affine import smooth_local_affine
from flask import Flask
app = Flask(_name_)
...
出现以下错误:

然后我尝试添加
context.pop()
,然后出现另一个错误

atexit中出错。\u运行\u退出功能:回溯(最近一次调用最后一次):
文件 “/home/yifang/anaconda3/envs/python3/lib/python3.6/site packages/pycuda-2017.1-py3.6-linux-x86_64.egg/pycuda/autoinit.py”, 第14行,完成 context.pop()pycuda.\u driver.LogicError:context::pop失败:设备上下文无效-无法弹出非当前上下文


有人知道如何在Flask环境中运行PyCuda吗?或者,我可以使用平滑的局部仿射特征而不使用PyCuda的任何替代方法?

让我在这里介绍一个解决方案,因为我已经尝试了很多解决方案,但仍然不起作用。幸运的是我找到了一个正确的答案

一些解决方案如

import pycuda.autoinit

如果您以简单程序的形式运行脚本,这些可能会起作用,但如果您使用flask或其他web服务器运行脚本,则会引发上下文错误。根据我的搜索,原因可能是flask服务器在收到请求时会产生新线程

在这种情况下,真正的解决方案非常简单,您只需添加如下代码:

with engine.create_execution_context() as context:
    ctx = cuda.Context.attach()
    inputs, outputs, bindings, stream = allocate_buffer()
    ctx.detach()

这对我有用

您能解决此问题吗?请尝试可能的副本
cuda.init 
device = cuda.Device(0) 
ctx = device.make_context() 
inputs, outputs, bindings, stream = allocate_buffer() 
ctx.pop()
with engine.create_execution_context() as context:
    ctx = cuda.Context.attach()
    inputs, outputs, bindings, stream = allocate_buffer()
    ctx.detach()