如何处理Python的线程。线程异常?

时间:2016-02-10 13:58:11

标签: python multithreading exception-handling python-multithreading

我正在使用Selenium运行两个Firefox实例并获取特定页面。 我正在使用threading.Thread为每个页面并行加载页面。 我还想在最大页面加载时间设置超时,在我的代码中使用browser.set_page_load_timeout()

我的整个代码如下:

from selenium import webdriver
from threading import Thread
from selenium.common.exceptions import TimeoutException


class Test():
        def __init__(self):
            browser = webdriver.Firefox()

            def load_page(browser, url):
                browser.set_page_load_timeout(20)
                browser.get(url)

            t = Thread(target=load_page, args=(browser, 'http://www.stackoverflow.com', ))
            t.start()
            t.join()

if __name__ == '__main__':
    try:
        Test()
    except TimeoutException:
        print "timeout reached"

尽管我的try except声明,我仍然遇到此错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Temp\test_b.py", line 13, in load_page
    browser.get(url)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 213, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
TimeoutException: Message: Timed out waiting for page load.
Stacktrace:
    at Utils.initWebLoadingListener/< (file:///c:/users/mgal/appdata/local/temp/tmpsasxck/extensions/fxdriver@googlecode.com/components/driver-component.js:9010)
    at WebLoadingListener/e (file:///c:/users/mgal/appdata/local/temp/tmpsasxck/extensions/fxdriver@googlecode.com/components/driver-component.js:5114)
    at WebLoadingListener/< (file:///c:/users/mgal/appdata/local/temp/tmpsasxck/extensions/fxdriver@googlecode.com/components/driver-component.js:5122)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/mgal/appdata/local/temp/tmpsasxck/extensions/fxdriver@googlecode.com/components/driver-component.js:621)

总之,如何从线程范围中捕获超时异常? 谢谢你!

2 个答案:

答案 0 :(得分:0)

你不能。您应该在线程中处理异常,因此您的函数应该看起来或多或少:

def load_page(browser, url):
    try:
        browser.set_page_load_timeout(20)
        browser.get(url)
    except TimeoutException:
        '''Handle me here'''

编辑:您实际要求的是:

from selenium import webdriver
from threading import Thread
from Queue import Queue
from selenium.common.exceptions import TimeoutException


class Test():
        def __init__(self, queue, url):
            browser = webdriver.Firefox()

            def load_page(browser, url):
                try:
                    browser.set_page_load_timeout(20)
                    browser.get(url)
                except Exception as e:
                    queue.put(e)
                else:
                    queue.put('OK or whatever the result you want')

            t = Thread(target=load_page, args=(browser, url, ))
            t.start()

if __name__ == '__main__':
    urls = ('http://www.stackoverflow.com', 'http://http://meta.stackoverflow.com/')
    queue = Queue()
    for url in urls:
        Test(queue, url)
    for i in range(len(urls)):
        result = queue.get()
        if isinstance(result, Exception):
            '''Handle exception preferably trying to determine the actual exception type'''
        else:
            '''Say cool cause everything is fine'''

答案 1 :(得分:0)

线程子进程在自己的堆栈中运行,因此如果没有消息/事件传递,这是不可能的。您可以使用python的Queue库(它是线程安全的)并将队列对象传递到子函数中,将其用作父级可以处理的事件池。