Python多进程 builtins.IOError: [Errno 22] 无效参数
我不太明白为什么会出现这个错误。我到处查找也没有找到解决办法。我决定试着让我的脚本使用多线程来运行,使用的是multiprocessing模块,如果我把那部分代码去掉,脚本就能正常运行。
于是我运行了调试器,结果在多线程代码上也没有遇到任何错误,这让我觉得有点奇怪。但是当我尝试正常运行脚本时,它在3.2.3版本下打印了:
Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate scratch.py]
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python32\Lib\multiprocessing\forking.py", line 369, in main
self = load(from_parent)
AttributeError: 'module' object has no attribute 'search_letters_in_words'
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 4.1\src\debug\tserver\_sandbox.py", line 122, in <module>
File "C:\Python32\Lib\multiprocessing\process.py", line 132, in start
self._popen = Popen(self)
File "C:\Python32\Lib\multiprocessing\forking.py", line 269, in __init__
to_child.close()
builtins.IOError: [Errno 22] Invalid argument
补充:我切换到3.3版本看看会发生什么,结果不稳定地抛出了这两个错误信息之一:
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate scratch.py]
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python33\Lib\multiprocessing\forking.py", line 344, in main
self = load(from_parent)
AttributeError: 'module' object has no attribute 'search_letters_in_words'
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 4.1\src\debug\tserver\_sandbox.py", line 122, in <module>
File "C:\Python33\Lib\multiprocessing\process.py", line 111, in start
self._popen = Popen(self)
File "C:\Python33\Lib\multiprocessing\forking.py", line 243, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Python33\Lib\multiprocessing\forking.py", line 160, in dump
ForkingPickler(file, protocol).dump(obj)
builtins.BrokenPipeError: [Errno 32] Broken pipe
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate scratch.py]
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python33\Lib\multiprocessing\forking.py", line 344, in main
self = load(from_parent)
AttributeError: 'module' object has no attribute 'search_letters_in_words'
补充 #2:在命令行调用时的错误信息:
D:\Python\PythonRepo>scratch.py > d:\download\error.txt
Traceback (most recent call last):
File "D:\Python\PythonRepo\scratch.py", line 122, in <module>
thread.start()
File "C:\Python32\Lib\multiprocessing\process.py", line 131, in start
from .forking import Popen
File "C:\Python32\Lib\multiprocessing\forking.py", line 180, in <module>
import _subprocess
ImportError: No module named '_subprocess'
这是我目前写的多进程代码。可能有问题(哈哈,谁知道呢,可能真的是!),但我不太确定哪里出错了,因为看起来是正确的(难道不是总是这样吗?)。
wordList = pickle.load( open( r'd:\download\allwords.pickle', 'rb')) #a list
combos = make_letter_combinations(3) # a list
split = split_list_multi(combos) #2 item tuple with a dict and a number
if __name__ == '__main__':
multiprocessing.freeze_support()
jobs = []
for num in range(split[1]):
listLetters = split[0][str(num)] #a list
thread = multiprocessing.Process(target=search_letters_in_words, args=(listLetters,wordList))
jobs.append(thread)
thread.start()
for j in jobs:
j.join()
补充:这是search_letters_in_words_函数的代码:
def search_letters_in_words(listOfLetters,wordlist):
results = {}
for letters in listOfLetters:
results[letters] = [i for i in wordlist if letters in i]
return results
如果有人能指出我哪里做错了,我会非常感激!
2 个回答
1
我遇到这个错误代码是因为另一个原因(我用的是Python 3和Windows 7)。从一个.ini文件中读取带引号的文件名导致了csv.DictWriter函数无法打开文件。去掉引号后问题就解决了:
...
[CSVFILE]
#CfgCsvNamePattern="C:\\temp\\PenSim-%%s.csv"
CfgCsvNamePattern=C:/temp/PenSim%%s.csv
...
3
试着从命令行运行这个脚本,而不是通过Wing IDE来运行:
python scripy.py