我在gui代码的一部分中设置了一个简单的生产者使用者模式。我正在尝试仅分析特定的消费者部分,以查看是否有进行优化的机会。但是,当尝试使用python -m cProfile -o out.txt myscript.py
运行代码时,我从Python的pickle
模块中抛出了一个错误。
File "<string>", line 1, in <module>
File "c:\python27\lib\multiprocessing\forking.py", line 374, in main
self = load(from_parent)
File "c:\python27\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "c:\python27\lib\pickle.py", line 858, in load
dispatch[key](self)
File "c:\python27\lib\pickle.py", line 880, in load_eof
raise EOFError
EOFError
代码中的基本模式是
class MyProcess(multiprocessing.Process):
def __init__(self, in_queue, msg_queue):
multiprocessing.Process.__init__(self)
self.in_queue = in_queue
self.ext_msg_queue = msg_queue
self.name == multiprocessing.current_process().name
def run(self):
## Do Stuff with the queued items
通常,这是从GUI方面完成任务的,但是出于测试目的,我将其设置如下。
if __name__ == '__main__':
queue = multiprocessing.Queue()
meg_queue = multiprocessing.Queue()
p = Grabber(queue)
p.daemon = True
p.start()
time.sleep(20)
p.join()
但是在尝试启动脚本后,我得到了上面的错误消息。
有没有办法解决该错误?
最佳答案
以我的理解,cProfile
模块在命令行上不能与multiprocessing
很好地配合使用。 (请参阅Python multiprocess profiling。)
解决此问题的一种方法是从代码内运行事件探查器-特别是,您需要为池中的每个进程设置一个不同的事件探查器输出文件。您可以通过调用cProfile.runctx('a+b', globals(), locals(), 'profile-%s.out' % process_name)
轻松完成此操作。
http://docs.python.org/2/library/profile.html#module-cProfile