擅长:python、mysql、java
<p>这是我的错,原因有二:</p>
<ol>
<li>代码没有保护,即没有<code>if __name__</code></li>
<li>奇怪的回溯是因为文件没有被保存。以前从来没有给我惹过麻烦,但在这种情况下的确是。</li>
</ol>
<p>修正了这两个错误。</p>
<p>最终测试代码:</p>
<pre><code>import concurrent.futures
nums = [1,2,3,4,5,6,7,8,9,10]
def f(x):
return x * x
def main():
# Make sure the map and function are working
print([val for val in map(f, nums)])
# Test to make sure concurrent map is working
with concurrent.futures.ProcessPoolExecutor() as executor:
print([val for val in executor.map(f, nums)])
if __name__ == '__main__':
main()
</code></pre>
<p>产出,如预期:</p>
<pre><code>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
</code></pre>