我试图在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中向全局列表添加/追加新结果?在
你有几个问题。首先,
list
(实际上应该重命名它,这样就不会隐藏内置的list
函数)是空的,因此运行函数0次。在
第二种情况是
ProcessPoolExecutor
在另一个进程中运行工作进程。worker将更新该进程的list
全局,而不是主进程中的全局。您应该使用其他池方法之一,如map
,并让您的工作线程返回其结果。在因为你的代码不可运行,所以我设计了一个不同的工作示例
^{pr2}$你的代码可以改成
相关问题 更多 >
编程相关推荐