uwsgi环境下,thread执行疑难问题,求解!

Mophyes 2015-12-11 02:44:50
在Python环境下做了一个利用线程异步执行的程序,但是http访问的request对象经过线程建立后传入总是有问题,其中的META经常是空的,偶尔会出现完整的情况,而且在生产环境下测试还会出现传入前后不一致的现象,fastcgi模式就没有问题,到底是什么原因呢?

代码:
import uwsgi
class ThreadSchem():
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def execute(self):
print self.args
print self.kwargs

def run(self):
import threading
print uwsgi.worker_id()

T = threading.Thread(target=self.execute)
T.setDaemon(True)
T.start()

class TS(ThreadSchem):
def execute(self):
print uwsgi.worker_id()
print '=================='
print 'args:'
print self.args
print '=================='
print 'kwargs:'
print self.kwargs

def after_browsing(request):
print 'request:'
print request
Schem = TS(request)
Schem.run()

return HttpResponse('')

运行结果:
request:
<WSGIRequest
path:/p,
GET:<QueryDict: {u'c': [u'1449727542.79'], u'rf': [u'']}>,
POST:<QueryDict: {}>,
COOKIES:{'Hm_lvt_dfd832c13ce8dc20db217ca2075711a8': '1449557364,1449640115,1449709325'},
META:{'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',
'HTTP_ACCEPT_LANGUAGE': 'zh-CN,zh;q=0.8',
'HTTP_CACHE_CONTROL': 'no-cache',
'HTTP_CONNECTION': 'keep-alive',
'HTTP_COOKIE': 'Hm_lvt_dfd832c13ce8dc20db217ca2075711a8=1449557364,1449640115,1449709325',
'HTTP_HOST': '211.137.44.167:8501',
'HTTP_PRAGMA': 'no-cache',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'PATH_INFO': u'/p',
'QUERY_STRING': 'c=1449727542.79&rf=',
'REMOTE_ADDR': '218.24.104.62',
'REMOTE_PORT': '61211',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': '/p?c=1449727542.79&rf=',
'SCRIPT_NAME': u'',
'SERVER_NAME': 'msp-w01.local',
'SERVER_PORT': '8501',
'SERVER_PROTOCOL': 'HTTP/1.1',
'UWSGI_ROUTER': 'http',
'uwsgi.core': 1,
'uwsgi.node': 'msp-w01.local',
'uwsgi.version': '2.0.11',
'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0x7f2b07dfc270>,
'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
'wsgi.input': <uwsgi._Input object at 0x7f2b07d19210>,
'wsgi.multiprocess': True,
'wsgi.multithread': True,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)}>
==================
args:
(<WSGIRequest
path:/p,
GET:<QueryDict: {u'c': [u'1449727542.79'], u'rf': [u'']}>,
POST:<QueryDict: {}>,
COOKIES:{'Hm_lvt_dfd832c13ce8dc20db217ca2075711a8': '1449557364,1449640115,1449709325'},
META:{}>,)
==================
kwargs:
{}
...全文
1315 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是Roger 2018-02-04
  • 打赏
  • 举报
回复
这是uwsgi的设置,你需要配置 enable-threads为true

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧