简体   繁体   English

python tempfile和多处理池错误

[英]python tempfile and multiprocessing pool error

I'm experimenting with python's multiprocessing. 我正在尝试python的多处理。 I struggled with a bug in my code and managed to narrow it down. 我努力解决代码中的错误,并设法缩小了范围。 However, I still don't know why this happens. 但是,我仍然不知道为什么会这样。 What I'm posting is just sample code. 我发布的只是示例代码。 If I import tempfile module and change tempdir, the code crashes at pool creation. 如果导入tempfile模块并更改tempdir,则代码在创建池时崩溃。 I'm using python 2.7.5 我正在使用python 2.7.5

Here's the code 这是代码

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]"

Here's error 这是错误

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

This code works fine. 此代码可以正常工作。

from multiprocessing import Pool
import tempfile as TF

TF.tempdir = "R:/"

def  f(x):
    return x*x

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

The bizarre thing is that, both times I don't do anything with TF.tempdir, but the one with the Pool doesn't work for some reason. 奇怪的是,两次我都没有使用TF.tempdir做任何事情,但是使用Pool的那个出于某种原因无法工作。

It is cool it looks like you have a name collision from what I can see in 很酷,看起来像您在我看到的名字中都有名字冲突

"C:\\Program Files\\PYTHON\\Lib\\multiprocessing\\connection.py" “ C:\\ Program Files \\ PYTHON \\ Lib \\ multiprocessing \\ connection.py”

It seems that multipprocessing is using tempfile as well 似乎multipprocessing也正在使用tempfile

That behavior should not happen but it looks to me like the problem is in line 66 of connection.py 该行为不应该发生,但在我看来,问题出在connection.py的第66行

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

I am still poking at this, I looked at globals after importing tempfile and then tempfile as TF, different names exist but now I am wondering about references, and so am trying to figure out if they point to the same thing. 我仍然对此感到不安,在导入tempfile和tempfile作为TF之后,我查看了全局变量,存在不同的名称,但是现在我想知道引用,因此试图弄清楚它们是否指向同一对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM