如何在Python的try/exception中追加全局列表变量?

2024-10-04 11:21:51 发布

您现在位置: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中向全局列表添加/追加新结果?在


Tags: 列表forwithcontenturllib2multiple全局versions
1条回答
网友
1楼 · 发布于 2024-10-04 11:21:51

你有几个问题。首先,list(实际上应该重命名它,这样就不会隐藏内置的list函数)是空的,因此

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

运行函数0次。在

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

因为你的代码不可运行,所以我设计了一个不同的工作示例

^{pr2}$

你的代码可以改成

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

相关问题 更多 >