urllib.urlopen不会在线程中执行
我正在使用线程,并且需要通过一个线程来下载一个网站。我还有一个线程负责向这个网站发送请求,但这个线程不需要等待回应。
class peticion(Thread):
def __init__(self, url):
Thread.__init__(self)
self.url = url
def run(self):
f = urllib.urlopen(self.url)
f.close()
这个线程工作得很好,不过那个需要等待回应的线程完成的时间有点随机,从5秒到2分钟不等,甚至可能永远都不会完成。这是相关的代码:
class playerConn(Thread):
def __init__(self, ev):
Thread.__init__(self)
self.ev = ev
def run(self):
try:
params = urllib.urlencode('''params go here''')
f = urllib.urlopen('''site goes here''')
resp = f.read()
f.close()
finally:
# do something with the response
无论我是否使用try...finally语句,它都不管用,urlopen
函数后面的代码就是执行不了。
我该怎么办呢?
1 个回答
0
看起来只是网址出了点问题,代码本身没啥问题,也没有做错什么。我猜你可能是网站那边出了点问题,比如说404错误之类的。你可以试着在本地打开一些东西,看看能不能正常运行。