繁体   English   中英

在uwsgi下,如何使用urllib2.urlopen连接到其他服务?

[英]Under uwsgi, how to use urllib2.urlopen to connect to other service ?

我正在使用nginx + uwsgi + flask 在flask中,我需要使用urllib2.urlopen()访问谷歌翻译API。 通过仅运行烧瓶的.py文件,我可以获得正确的结果。 但是一旦我连接到uWSGI,它总是会出现Segmentation Fault。

我的uWSGI.ini:

[uwsgi]
socket = 127.0.0.1:10001
chdir = /Users/xingshi/Workspace/misc/howToSpeak/flask/py
wsgi-file = api.py 
pyargv = ../config/mac.cfg
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:10002
master = true
harakiri = 60

调用urllib2的Python代码段

query = urllib.urlencode(fields)
url = 'https://www.googleapis.com/language/translate/v2?'+query
print url
json_str = urllib2.urlopen(url).read()
print json_str

细分错误:

https://www.googleapis.com/language/translate/v2?q=%E6%88%91%E7%88%B1%E4%BD%A0&source=zh&target=en&key=AIzaSyADlrkxuZ47AGABAmqk8JsIHPkU_3WS4Wg&format=text
!!! uWSGI process 32069 got Segmentation Fault !!!
*** backtrace of 32069 ***
0   uwsgi                               0x000000010d0664e0 uwsgi_backtrace + 48
1   uwsgi                               0x000000010d066a13 uwsgi_segfault + 51
2   libsystem_platform.dylib            0x00007fff89f785aa _sigtramp + 26
3   CoreFoundation                      0x00007fff8799b6e9 CFRelease + 1097
4   CoreFoundation                      0x00007fff879c36f1 withDaemonConnection + 273
5   CoreFoundation                      0x00007fff879c316b -[CFPrefsPlistSource alreadylocked_synchronize] + 587
6   CoreFoundation                      0x00007fff879c2e63 _copyValueForKey + 131
7   CoreFoundation                      0x00007fff879c2db7 -[CFPrefsPlistSource copyValueForKey:] + 71
8   CoreFoundation                      0x00007fff879c2c15 -[CFPrefsSearchListSource alreadylocked_copyValueForKey:] + 149
9   CoreFoundation                      0x00007fff879c2b4f -[CFPrefsSource copyValueForKey:] + 79
10  CoreFoundation                      0x00007fff879c2ae0 __CFPreferencesCopyAppValue_block_invoke + 32
11  CoreFoundation                      0x00007fff879bccbe +[CFPrefsSearchListSource withSearchListForIdentifier:perform:] + 846
12  CoreFoundation                      0x00007fff879bc928 CFPreferencesCopyAppValue + 168
13  SystemConfiguration                 0x00007fff8e6de76e SCDynamicStoreCopyProxiesWithOptions + 153
14  SystemConfiguration                 0x00007fff8e6de6c0 SCDynamicStoreCopyProxies + 27
15  _scproxy.so                         0x000000010dbbeb9e get_proxies + 14
16  libpython2.7.dylib                  0x000000010d6351ef PyEval_EvalFrameEx + 12338
17  libpython2.7.dylib                  0x000000010d638864 _PyEval_SliceIndex + 829
18  libpython2.7.dylib                  0x000000010d6354d4 PyEval_EvalFrameEx + 13079
19  libpython2.7.dylib                  0x000000010d638864 _PyEval_SliceIndex + 829
20  libpython2.7.dylib                  0x000000010d6354d4 PyEval_EvalFrameEx + 13079
21  libpython2.7.dylib                  0x000000010d632093 PyEval_EvalCodeEx + 1641
22  libpython2.7.dylib                  0x000000010d5d9796 PyFunction_SetClosure + 809
23  libpython2.7.dylib                  0x000000010d5bbf72 PyObject_Call + 101
24  libpython2.7.dylib                  0x000000010d5c69a7 PyMethod_New + 1200
25  libpython2.7.dylib                  0x000000010d5bbf72 PyObject_Call + 101

只需使用urllib3

url = 'https://www.googleapis.com/language/translate/v2?'+query
http = urllib3.PoolManager()
r = http.request('GET',url)
json_str = r.data

解决方法:

https://blog.yimingliu.com/2015/07/22/python-multiprocessing-code-crashes-on-os-x-under-ipython/

使用“ no_proxy ='*'”运行代码似乎可以防止尝试加载代理设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM