开发者

python threading - a problem

I can't figure out the problem in this code.

class Threader(threading.Thread):
    def __init__(self, queue, url, host):
        threading.Thread.__init__(self)
        self.queue = queue
        self.url = url
        self.host = host

    def run(self):

        print self.url # http://www.stackoverflow.com            

        开发者_JS百科with contextlib.closing(urllib2.urlopen(self.url)) as u:
            source = u.read()

        print "hey" # this is not printing!

        source = self.con()

        doc = Document(source)

        self.queue.put((doc, self.host))         

When I run this code, print self.url succesfully outputs the url but print "hey" is not working. So basically, (I believe) there is something with contextlib which is blocking the code. I also tried the conventional urlopen method without using contextlib, but it doesn't work either. Furthermore, I tried try - except but the program doesn't raise any error. So what may be the problem here?


Your Code doesn't work, I have taken the liberty to adapt it a bit (imports, also it doesn't know about Document and self.con), and make it compatible with python2 (that's what I use here at the moment) - it works:

from __future__ import with_statement
import threading, Queue, urllib2, contextlib

class Threader(threading.Thread):

    def __init__(self, queue, url, host):
        threading.Thread.__init__(self)
        self.queue = queue
        self.url = url
        self.host = host

    def run(self):
        print self.url
        with contextlib.closing(urllib2.urlopen(self.url)) as u:
            source = u.read()
        print "hey"

if '__main__'==__name__:
    t = Threader(Queue.Queue(), 'http://www.stackoverflow.com', '???')
    t.start()
    t.join()

EDIT: works also with "with" and contextlib


Since the problem persists with only using urllib, the most probable cause is that the url you are trying to open does not response.

You should try to

  1. open the url in a browser or a simple web client (like wget on linux)
  2. set the timeout parameter of urllib2.urlopen
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜