python 线程 - 一个问题

发布于 2024-12-03 04:31:05 字数 817 浏览 3 评论 0原文

我无法找出这段代码中的问题。

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            

        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))         

当我运行此代码时,print self.url 成功输出 URL,但 print "hey" 不起作用。所以基本上,(我相信)contextlib 中有一些东西阻塞了代码。我也尝试了传统的 urlopen 方法而不使用 contextlib ,但它也不起作用。此外,我尝试了 try - except 但程序没有引发任何错误。那么这里可能存在什么问题呢?

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            

        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等数载,海棠开 2024-12-10 04:31:05

你的代码不起作用,我冒昧地对其进行了一些修改(导入,它也不知道 Document 和 self.con),并使其与 python2 兼容(这就是我现在在这里使用的) - 它有效:

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()

编辑:也适用于“with”和 contextlib

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

拒绝两难 2024-12-10 04:31:05

由于仅使用 urllib 时问题仍然存在,最可能的原因是您尝试打开的 url 没有响应。

您应该尝试

  1. 在浏览器或简单的 Web 客户端(如 Linux 上的 wget)中打开 url,
  2. 设置 urllib2.urlopentimeout 参数

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文