多处理抛出属性错误,为什么?
Multiprocessing throws Attribute Error, why?
出于某种我看不到的原因,此代码引发错误:AttributeError: __exit__
代码很简单:
import re, string, math, numpy, time, os.path, itertools, matplotlib, subprocess, shutil, sys, scipy.spatial.distance, multiprocessing, threading, ctypes
from functools import partial
from multiprocessing.sharedctypes import Value, Array
from multiprocessing import Process, Lock
def main():
with multiprocessing.Pool(8) as myPool:
print("1")
if __name__ == '__main__':
main()
各种导入行用于我在多线程上使用的其他代码的其他内容。然而,这是我试图分析以了解诀窍的简单 'sample' 代码。我假设它在打开 with 块时遇到了一些问题,但我不明白为什么。 Python 2.7 不以这种方式实现多处理吗?这都是我见过的例子。有没有其他方法我应该实现这样的东西?
我希望能够向一组线程抛出一堆输入略有不同的函数调用并将它们取回,但如果我无法启动线程,那还有很长的路要走。
对 2.7 标准进行一些简单的修改,这很有效。
import multiprocessing
def somefunc(x):
print(x)
def main():
myPool = multiprocessing.Pool(8):
myPool.map(range(8))
if __name__ == '__main__':
main()
出于某种我看不到的原因,此代码引发错误:AttributeError: __exit__
代码很简单:
import re, string, math, numpy, time, os.path, itertools, matplotlib, subprocess, shutil, sys, scipy.spatial.distance, multiprocessing, threading, ctypes
from functools import partial
from multiprocessing.sharedctypes import Value, Array
from multiprocessing import Process, Lock
def main():
with multiprocessing.Pool(8) as myPool:
print("1")
if __name__ == '__main__':
main()
各种导入行用于我在多线程上使用的其他代码的其他内容。然而,这是我试图分析以了解诀窍的简单 'sample' 代码。我假设它在打开 with 块时遇到了一些问题,但我不明白为什么。 Python 2.7 不以这种方式实现多处理吗?这都是我见过的例子。有没有其他方法我应该实现这样的东西?
我希望能够向一组线程抛出一堆输入略有不同的函数调用并将它们取回,但如果我无法启动线程,那还有很长的路要走。
对 2.7 标准进行一些简单的修改,这很有效。
import multiprocessing
def somefunc(x):
print(x)
def main():
myPool = multiprocessing.Pool(8):
myPool.map(range(8))
if __name__ == '__main__':
main()