如何在Python中的try / except异常中附加全局列表变量?

时间:2016-06-13 16:28:25

标签: python

我尝试使用try / except异常中的新单词追加全局列表变量,但在try / except之后我得到空列表。

    list = []                              # created empty list with global scope
    def try_multiple_operations(j):
            try:
                jopen = urllib2.urlopen(j) # opened url for parsing content
                versions = jopen.read()    # read and save to variable
                version = pq(versions)     # filtering content with pyquery
                ....                       # pyquery operations
                list.append(version)
            except urllib2.URLError:       # urllib2 exception
                list.append('0.0')
            except urllib2.HTTPError:      # urllib2 exception
                list.append('0.0')
executor = concurrent.futures.ProcessPoolExecutor(5)
futures = [executor.submit(try_multiple_operations, j) for j in list]
concurrent.futures.wait(futures)
print len(list)                        # 0 elements

最后我得到了空名单。如何在try / except?

中添加/追加新结果到全局列表

1 个答案:

答案 0 :(得分:3)

你有几个问题。首先,list(实际上应该重命名以便不影响内置的list函数)是空的,所以

futures = [executor.submit(try_multiple_operations, j) for j in list]

运行你的函数零次。

第二个是ProcessPoolExecutor在另一个进程中运行worker。工作人员将更新该进程的list全局进程,而不是主进程中的进程。您应该使用其他池方法,例如map,并让您的工作人员返回其结果。

由于您的代码不可运行,我编写了一个不同的工作示例

import concurrent.futures

def try_multiple_operations(j):
    try:
        if j % 2:
            raise ValueError('oops')
        return '%d even' % j
    except ValueError:
        return '%d odd' % j

executor = concurrent.futures.ProcessPoolExecutor(5)
my_list = list(executor.map(try_multiple_operations, range(10)))
print(my_list)

您的代码可以更改为

def try_multiple_operations(j):
        try:
            jopen = urllib2.urlopen(j) # opened url for parsing content
            versions = jopen.read()    # read and save to variable
            version = pq(versions)     # filtering content with pyquery
            ....                       # pyquery operations
            return version
        except urllib2.URLError:       # urllib2 exception
            return '0.0'
        except urllib2.HTTPError:      # urllib2 exception
            return '0.0'

url_list = [   ...something... ]
executor = concurrent.futures.ProcessPoolExecutor(5)
my_list = list(executor.map(try_multiple_operations, url_list)
print len(my_list)                        # 0 elements