3
我有一個在Linux中運行良好的Python 3腳本。當我在Windows下運行它,我每一次我嘗試播放聲音時得到以下回溯:我的異常來源是什麼?
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files (x86)\Python35-32\lib\multiprocessing\spawn.py", line 106, in spawn_main
exitcode = _main(fd)
File "C:\Program Files (x86)\Python35-32\lib\multiprocessing\spawn.py", line 116, in _main
self = pickle.load(from_parent)
EOFError: Ran out of input
我看了,我認爲可能是負責這個的代碼,但我沒有看到如何multiprocessing
甚至在這裏是相關的,因爲我已經抓到了EOFError,或者在我看來。 (請注意,這個例外不會導致我的腳本退出。)
我最大的問題是沒有關於Python認爲問題發生在哪裏的信息。它指的是File "<string>", line 1, in <module>
,這是無益的。我怎樣才能找出異常實際上升起的位置?
下面是我認爲方法必須是負責任的,因爲它是我用multiprocessing
的唯一的地方:
# imported earlier:
import multiprocessing as mp
def _play(self, name):
'''Plays the sound given by name'''
if self.silent:
return True
def play_sound(q=None): # Called in a separate process so playing the sound doesn't block anything else.
'''q is a multiprocessing.Queue object for interprocess communication.'''
try:
snd = self.sounds[name]
if self._use_subprocess:
s = subprocess
return s.call([self.player, snd], stdout=s.DEVNULL, stderr=s.DEVNULL, timeout=20)
else:
return self.player(snd, self._winsound_options)
except (KeyboardInterrupt, EOFError):
sys.exit(0)
except subprocess.TimeoutExpired as e:
#if q:
# q.put(e)
sys.stderr.write('\nSOUND ERROR: Unable to play the sound "{}": Timeout expired.\n\n'.format(snd))
sys.exit(1)
except FileNotFoundError:
sys.stderr.write('\nSOUND ERROR: Unable to locate the sound player "{}".\n\n'.format(self.player))
sys.exit(2)
except EOFError:
pass
#q = mp.Queue()
proc_name = 'Play sound: {}; time: {}'.format(name, datetime.datetime.now().strftime('%I:%M:%S %p'))
try:
proc = mp.Process(target=play_sound, name=proc_name)#, args=(q,))
proc.start()
# On Windows, multiprocessing attempts to pickle this method, which fails
# because it's called by an inner function defined in __getattr__. We'll
# Skip using multiprocessing in this case.
# References: https://github.com/ouspg/trytls/issues/196 and http://stackoverflow.com/a/36995008/713735
except (AttributeError, EOFError):
play_sound()
「File」「'部分可以來自運行python代碼片段,直接從命令行運行'python -c'這裏有一些代碼' '。你是否在你的啓動腳本中的任何位置執行此操作? –
這的確是它的樣子,但我可以向你保證我沒有那樣做。其他回溯我看起來很正常。 –
您的代碼存在一個潛在的問題,那就是在Windows上使用'multiprocessing''',需要將主進程的代碼放在'if __name__ =='__main __':'塊中。請參閱[_multiprocessing編程準則_](https:/)的[Windows](https://docs.python.org/2/library/multiprocessing.html#windows)部分的主模塊**安全導入部分/docs.python.org/2/library/multiprocessing.html#programming-guidelines)。 – martineau