uwsgi环境下,thread执行疑难问题,求解!
在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:
{}
代码:
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:
{}
...全文
相关推荐
Django uwsgi Nginx 的生产环境部署详解
主要介绍了Django uwsgi Nginx 的生产环境部署详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
centos下使用Nginx+uWsgi部署Python Flask项目的详细配置
在centos系统下,使用Nginx反向代理和uWsgi服务器部署Python Flask项目,这里记录了我配置的整个过程,亲测可用
详解Ubuntu环境下部署Django+uwsgi+nginx总结
主要介绍了Ubuntu环境下部署Django+uwsgi+nginx总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Django+Uwsgi+Nginx如何实现生产环境部署
主要介绍了Django+Uwsgi+Nginx如何实现生产环境部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Apache+uWSGI+Django+Mysql环境配置手册
在Windows环境中,配置Apache+uWSGI+Django+Mysql环境。


