Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多处理抛出属性错误,为什么?_Python_Multithreading_Python 2.7_With Statement - Fatal编程技术网

Python 多处理抛出属性错误,为什么?

Python 多处理抛出属性错误,为什么?,python,multithreading,python-2.7,with-statement,Python,Multithreading,Python 2.7,With Statement,由于某种我看不到的原因,这段代码抛出了一个错误:AttributeError:\uuu 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 mu

由于某种我看不到的原因,这段代码抛出了一个错误:
AttributeError:\uuu 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()
不同的导入行用于我在多线程处理中使用的其他代码。然而,这是一个简单的“示例”代码,我正试图通过分析来了解其中的诀窍。我想它在打开模块时遇到了一些问题,但我不明白为什么。Python2.7是否不以这种方式实现多处理?这些都是我见过的例子。有没有其他方法可以让我实现这样的事情


我希望能够向一组线程抛出一组输入稍有不同的函数调用,然后将它们取回,但如果我无法启动线程,那就太遥远了。

对2.7标准进行一些简单的修改,这就行了

import  multiprocessing

def somefunc(x):
    print(x)

def main():
    myPool = multiprocessing.Pool(8):
    myPool.map(range(8))

if __name__ == '__main__':
   main()

上下文管理功能直到3.3才添加。在您看到的所有示例中,这可能都是因为它们假设了Python 3,除非您有充分的理由不这样做,否则您可能应该使用Python 3。我们使用的一些代码非常大,很难升级到Python 3,而这里的一些代码也拒绝升级(我不是其中之一)。但由于我的代码也需要为它们运行,不幸的是,它需要兼容。所以我假设这意味着我必须将它用作一个对象,即
obj=multiprocessing.Pool(8)
,然后
obj.close()
杀死它?