我有以下程序,它是一个 Boggle 求解器:
import logging
import multiprocessing
from random import choice
from string import ascii_uppercase
def get_grid(size=None, letters=None):
if size:
grid = {(x, y): choice(ascii_uppercase) for x in range(size[0]) for y in
range(size[1])}
elif letters:
grid = {}
rows = letters.split()
for y, row in enumerate(rows):
for x, letter in enumerate(row):
grid[x, y] = letter
return grid
def print_grid(grid):
s = ''
for y in range(size[1]):
for x in range(size[0]):
s += grid[x, y] + ' '
s += '\n'
print s
def get_neighbours():
neighbours = {}
for position in grid:
x, y = position
positions = [(x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x + 1, y),
(x + 1, y + 1), (x, y + 1), (x - 1, y + 1), (x - 1, y)]
neighbours[position] = [p for p in positions if
0 <= p[0] < size[0] and 0 <= p[1] < size[1]]
return neighbours
def get_wordlist():
stems = set()
wordlist = set()
with open('words.txt') as f:
for word in f:
word = word.strip().upper()
wordlist.add(word)
for i in range(len(word)):
stems.add(word[:i + 1])
return wordlist, stems
def path_to_word(path):
return ''.join([grid[p] for p in path])
def search(path):
word = path_to_word(path)
if word not in stems:
return
if word in wordlist:
paths.append(path)
for next_pos in neighbours[path[-1]]:
if next_pos not in path:
search(path + [next_pos])
def get_words():
for position in grid:
logging.info('searching %s' % str(position))
search([position])
return {path_to_word(p) for p in paths}
if __name__ == '__main__':
logging.basicConfig(level=logging.WARNING)
size = 4, 4
grid = get_grid(size=size)
print_grid(grid)
neighbours = get_neighbours()
wordlist, stems = get_wordlist()
paths = []
#words = get_words()
pool = multiprocessing.Pool(processes=4)
results = pool.map(search, grid)
words = [path_to_word(p) for p in paths]
print sorted(words, key=len, reverse=True)
当我运行它时,我收到以下消息:
Traceback (most recent call last):
File "C:/Users/will/PycharmProjects/boggle/boggle.py", line 103, in <module>
results = pool.map(search, grid)
File "C:\Python27\lib\multiprocessing\pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "C:\Python27\lib\multiprocessing\pool.py", line 567, in get
raise self._value
NameError: global name 'grid' is not defined
当我不尝试使用多处理模块时,程序运行良好,即注释掉 3 行:
pool = multiprocessing.Pool(processes=4)
results = pool.map(search, grid)
words = [path_to_word(p) for p in paths]
并取消注释:
words = get_words()
我猜多处理会以某种方式改变变量范围?