调用 urllib.urlopen 时的 Trace/BPT 陷阱
由于某种原因,我在调用 urllib.urlopen 时收到 Trace/BPT 陷阱错误。我尝试了 urllib
和 urllib2
,结果相同。以下是引发错误的代码:
def get_url(url):
from urllib2 import urlopen
if not url or not url.startswith('http://'): return None
return urlopen(url).read() # FIXME!
我应该补充一点,该代码是在带有 web.py 的 CherryPy Web 服务器上运行的。
有人请求追溯。不幸的是,没有。 Trace/BPT trap
输出到终端,进程终止。例如
dloewenherz@andros project $ sudo ./index.py 80
http://0.0.0.0:80/
# Here I visit the page which contains the get_url(url) method
Trace/BPT trap
dloewenherz@andros project $
编辑:我正在运行 OS X 10.6.2、web.py 0.33、Python 2.6.2 和 CherryPy 3.1.2。
For some reason I'm getting a Trace/BPT trap error when calling urllib.urlopen
. I've tried both urllib
and urllib2
with identical results. Here is the code which throws the error:
def get_url(url):
from urllib2 import urlopen
if not url or not url.startswith('http://'): return None
return urlopen(url).read() # FIXME!
I should add that this code is running on a CherryPy webserver with web.py.
Someone requested a traceback. Unfortunately, there is none. Trace/BPT trap
is outputted to the terminal and the process terminates. E.g.
dloewenherz@andros project $ sudo ./index.py 80
http://0.0.0.0:80/
# Here I visit the page which contains the get_url(url) method
Trace/BPT trap
dloewenherz@andros project $
Edit: I am running OS X 10.6.2, web.py 0.33, Python 2.6.2, and CherryPy 3.1.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在主文件顶部添加以下几行解决了问题:
换句话说,导入 urllib2 模块还不够,您实际上需要在主线程中创建 opener。
Adding the following lines to the top of the main file solved the problem:
In other words, it is not enough to import the urllib2 module but you actually need to create the opener in the main thread.
您在 OS X 10.6 下运行吗?显然,线程和导入模块第一次并不能很好地协同工作。看看线程外是否不能导入urllib2?
以下线程中有更多详细信息: Trace/BPT trap with Python threading module
我会尝试将 urllib 的导入移动到同一文件的顶部,或者因为这似乎只是在线程中第一次导入模块时出现问题,所以也将其导入其他地方,就像在与 main() 函数相同的文件中。
编辑:您正在运行哪些版本的 OS X、Python、CherryPy 和 web.py?我使用的是 OS X 10.5.8、Python 2.6、CherryPy 3.1.2 和 web.py 0.33,无法使用以下代码重现您的问题:
此代码是否足以重现您的问题?如果没有,我需要更多信息才能提供帮助。
Are you running this under OS X 10.6? Apparently threads and importing modules for the first time does not play well together there. See if you can't import urllib2 outside of the thread?
There are a few more details in the following thread: Trace/BPT trap with Python threading module
I'd try either moving the import of urllib to the top of the same file or, since it seems to be a problem only with importing a module for the first time in a thread, import it somewhere else as well, like in the same file as your main() function.
Edit: Which versions of OS X, Python, CherryPy and web.py are you running? I'm using OS X 10.5.8, Python 2.6, CherryPy 3.1.2 and web.py 0.33 and can't reproduce your problem using the below code:
Is this code enough to reproduce the problem on your end? If not, I need more information in order to be of help.