我有一个在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()
答案 0 :(得分:0)
我仍然不理解我所获得的追溯,但我通过改变来解决这个问题:
proc = mp.Process(target=play_sound, name=proc_name)
proc.start()
为:
if os.name == 'nt':
play_sound()
else:
proc = mp.Process(target=play_sound, name=proc_name)
proc.start()
是的,我以这种方式失去了功能,但由于缺少一个好的终端,脚本已经在Windows上瘫痪了。 (而且我对Windows并不十分了解。)