以编程方式终止uwsgi工作者 [英] Terminating a uwsgi worker programmatically

查看:107
本文介绍了以编程方式终止uwsgi工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要模拟" HTTP超时.简而言之,在这种情况下:

In my application I need to "simulate" a HTTP timeout. Simply put, in this scenario:

client -> myapp -> server

clientmyapp建立HTTP POST连接,然后将其转发到server.但是,由于网络问题或类似问题,server无法响应.我被困在client中的开放TCP会话中,我需要删除它.

client makes a HTTP POST connection to myapp which forwards it to server. However, server does not respond due to network issues or similar problems. I am stuck with an open TCP session from client which I'll need to drop.

我的应用程序使用web.py,nginx和uwsgi.

My application uses web.py, nginx and uwsgi.

我无法返回自定义的HTTP错误,例如418 I am a teapot-它必须是连接超时,以尽可能接近地反映server的行为.

I cannot return a custom HTTP error such as 418 I am a teapot - it has to be a connection timeout to mirror server's behaviour as closely as possible.

(我猜)一个hack-y解决方案可能只是time.wait(),直到client断开连接,但这将使用uwsgi线程,并且我认为这可能导致资源匮乏,因为可能会出现server超时发生其他连接.在此处中指出了另一种方法,但是这种解决方案意味着返回client的东西,不是我的情况.

One hack-y solution could be (I guess) to just time.wait() until client disconnects but this would use a uwsgi thread and I have a feeling it could lead to resource starvation because a server timeout is likely to happen for other connections. Another approach is pointed out here however this solution implies returning something to client, which is not my case.

所以我的问题是:是否有一种优雅的方法可以通过编程方式从python代码中杀死uwsgi工作者?

So my question is: is there an elegant way to kill a uwsgi worker programmatically from python code?

到目前为止我已经找到

  • set_user_harakiri(N) 我可以将其与time.sleep(N+1)结合使用.但是,在这种情况下,uwsgi会检测到harakiri并尝试重新生成工作程序.
  • worker_id() 但我不确定如何处理-我找不到太多使用它的文档
  • 按照 disconnect() 似乎没有任何作用,因为代码继续并返回到client
  • suspend() 可以挂起实例,但NGINX返回样板错误页面
  • set_user_harakiri(N) which I could combine with a time.sleep(N+1). However in this scenario uwsgi detects the harakiri and tries re-spawning the worker.
  • worker_id() but I'm not sure how to handle it - I can't find much documentation on using it
  • A suggestion to use connection_fd() as explained here
  • disconnect() which does not seem to do anything, as the code continues and returns to client
  • suspend() does suspend the instance, but NGINX returns the boilerplate error page

还有其他想法吗?

更新

事实证明,这要复杂得多.如果我只是关闭套接字或与uwsgi断开连接,nginx Web服务器将检测到服务器错误"并返回500样板错误页面.而且,我不知道如何告诉nginx停止如此有用.

Turns out it's more complicated than that. If I just close the socket or disconnect from uwsgi the nginx web server detects a 'server error' and returns a 500 boilerplate error page. And, I do not know how to tell nginx to stop being so useful.

推荐答案

答案是两者的结合.

  1. 在python应用中,返回444
  2. 按照关于此答案的说明配置nginx,即使用uwsgi_intercept_errors指令.
  1. From the python app, return 444
  2. Configure nginx as explained on this answer i.e. using the uwsgi_intercept_errors directive.

这篇关于以编程方式终止uwsgi工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆