python tempfile和多处理池错误

时间:2016-01-03 18:07:21

标签: python multiprocessing

我正在尝试python的多处理。我在代码中遇到了一个错误,并设法缩小范围。但是,我仍然不知道为什么会这样。我发布的只是示例代码。如果我导入tempfile模块并更改tempdir,则代码会在创建池时崩溃。我正在使用python 2.7.5

这是代码

from multiprocessing import Pool
import tempfile

tempfile.tempdir = "R:/" #REMOVING THIS LINE FIXES THE ERROR

def  f(x):
 return x*x

if __name__ == '__main__':
 pool = Pool(processes=4)              # start 4 worker processes
 result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously
 print result.get(timeout=1)           # prints "100" unless your computer is *very* slow
 print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"

这是错误

R:\>mp_pool_test.py
Traceback (most recent call last):
  File "R:\mp_pool_test.py", line 11, in <module>
pool = Pool(processes=4)              # start 4 worker processes
File "C:\Python27\lib\multiprocessing\__init__.py", line 232, in Pool
return Pool(processes, initializer, initargs, maxtasksperchild)
File "C:\Python27\lib\multiprocessing\pool.py", line 138, in __init__
self._setup_queues()
File "C:\Python27\lib\multiprocessing\pool.py", line 233, in _setup_queues
self._inqueue = SimpleQueue()
File "C:\Python27\lib\multiprocessing\queues.py", line 351, in __init__
self._reader, self._writer = Pipe(duplex=False)
File "C:\Python27\lib\multiprocessing\__init__.py", line 107, in Pipe
return Pipe(duplex)
File "C:\Python27\lib\multiprocessing\connection.py", line 223, in Pipe
1, obsize, ibsize, win32.NMPWAIT_WAIT_FOREVER, win32.NULL
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect

此代码工作正常。

from multiprocessing import Pool
import tempfile as TF

TF.tempdir = "R:/"

def  f(x):
    return x*x

if __name__ == '__main__':
 print("test")

奇怪的是,两次我都没有对TF.tempdir做任何事情,但是那个带有Pool的人因某种原因无效。

1 个答案:

答案 0 :(得分:0)

看起来你在

中看到的名字发生冲突很酷

&#34; C:\ Program Files \ PYTHON \ Lib \ multiprocessing \ connection.py&#34;

多处理似乎也在使用tempfile

这种行为不应该发生,但它看起来像问题是在connection.py

的第66行
elif family == 'AF_PIPE':
    return tempfile.mktemp(prefix=r'\\.\pipe\pyc-%d-%d-' %
                           (os.getpid(), _mmap_counter.next()))

我仍然在讨论这个问题,我在导入tempfile之后查看全局变量,然后将tempfile作为TF查看,存在不同的名称,但现在我想知道引用,所以我想弄清楚它们是否指向同一个东西。 / p>