我使用的是rails4,我想在编辑一个控制器之后执行一个Python脚本
我做过:
if @a_serverconfiguration.update(a_serverconfiguration_params)
format.html { redirect_to @a_serverconfiguration, notice: 'Device configuration was successfully updated. Next time, you have to access to new ip configuration. Rebooting device, this would take a while...' }
format.json { render :show, status: :ok, location: @a_serverconfiguration }
Thread.new do
exec "sudo python /home/pi/Desktop/starting.py"
end
else
format.html { render :edit }
format.json { render json: @a_serverconfiguration.errors, status: :unprocessable_entity }
end
end
但在执行python脚本之前,我看不到显示页面。此脚本在10秒后重新启动,因此当用户更新控制器时,无法看到消息“Device configuration was successfully updated”(设备配置已成功更新)。下一次,您必须访问新的ip配置。重新启动设备,这将需要一段时间…'。韦布瑞克等了10秒钟就下去了
我试过Webrick和thin,他们都是这样做的
如果我理解的很好:您希望您的控制器在python脚本完成之前呈现一个响应
使用线程允许同时运行多个代码块,我认为您希望像执行后台作业一样执行python脚本
相反,要使用线程,请查看[delayed\u job gem][1]。这个gem在后台封装并执行更长的任务
相关问题 更多 >
编程相关推荐