开发者

urllib.urlopen won't execute on a thread

I'm working with threads and I need to download a website with a thread. I also have a thread that sends the petition to the site but doesn't wait for an answer. The one that doesn't wait is this:

class peticion(Thread):
    def __init__(self, url):
        Thread.__init__(self)
        self.url = url

    def run(self):
        f = urllib.urlopen(self.url)
        f.close()

This one works correctly, however the one that has to wait for the response takes something like a random time to complete, from 5 seconds to 2 minutes, or it may never finish. This is the class:

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

Whether or not I use the try...finally statement it doesn't work, the code after the url开发者_开发知识库open function won't get to execute.

What can I do?


It appear to just be a problem with the URL, the code is fine and it does not appear to do something wrong. I bet you have some type of problem from the website, maybe a 404 or similar. Try opening something in localhost, just to test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜