使用多处理时python matplotlib动画保存错误

使用多处理时python matplotlib动画保存错误,python,animation,matplotlib,python-multiprocessing,Python,Animation,Matplotlib,Python Multiprocessing,我正在创建一个matplotlib动画,它通过文件中的一系列图像运行。我正在可视化的文件通常相当大,并且每个图像堆栈都有相当长的加载时间(~5秒)。我已经通过交错加载过程和多处理成功地使动画平稳运行,但是我在将动画保存为视频文件时遇到了问题 代码如下: from matplotlib import animation import pylab as plt import numpy as np import multiprocessing as mp import logging logger

我正在创建一个matplotlib动画,它通过文件中的一系列图像运行。我正在可视化的文件通常相当大,并且每个图像堆栈都有相当长的加载时间(~5秒)。我已经通过交错加载过程和多处理成功地使动画平稳运行,但是我在将动画保存为视频文件时遇到了问题

代码如下:

from matplotlib import animation
import pylab as plt
import numpy as np
import multiprocessing as mp
import logging
logger = mp.log_to_stderr(logging.INFO)
import time

def qloader(queue, threshold=100, nfiles=3):
    '''trigger a load process if number of items in queue drops below threshold'''
    while nfiles:
        if queue.qsize() < threshold:
            logger.info( 'qsize {}'.format(queue.qsize()) )

            time.sleep( 1 )     #pretend to load data
            data = np.random.rand(25,100,100)

            logger.info( 'Adding data to queue' )
            for d in data:
                queue.put(d)
            logger.info( 'Done adding data!' )
            nfiles -= 1
    else:
        queue.put( None )        #sentinal

def update(frame, im, queue):
    '''update the image'''
    logger.info( 'Updating frame %d'%frame )
    data = queue.get()
    if data is None:
        print( 'Queue is empty!' )
        return

    im.set_data( data )
    return im


#create data queue
mgr = mp.Manager()
queue = mgr.Queue()
threshold = 20          #

#start load process
p = mp.Process( name='loader', target=qloader, args=(queue, threshold) )
p.start()

#start animation
fig, ax = plt.subplots()
im = ax.imshow( np.random.rand(100,100) )
ani = animation.FuncAnimation( fig, update, frames=75, interval=100, repeat=0, fargs=(im, queue) )
ani.save('foo.mp4', 'ffmpeg')
我尝试过用不同的写入器和编码器以不同的文件格式保存,结果基本相同

此问题仅在使用
多处理
加载数据时发生。如果我只是使用
data=np.random.rand(75100100)
创建数据,动画保存不会出现问题


问题:如何使
matplotlib.animation
多处理一起播放?

默认情况下
动画。MovieWriter
使用
子进程.PIPE
将帧馈送给编写器。由于某种原因,当使用
多处理时,这似乎不起作用。将最后一行更改为
ani.save('foo.mp4','ffmpeg\u文件')

告诉编写者在编写电影之前将帧临时保存到光盘,哪一方会解决问题。

即使队列为空,也尝试返回
im
。已尝试。失败。据我所知,
vlc
demux错误表示数据丢失。输出文件的大小只有几个字节,所以我认为没有一个帧实际上被写入到光盘中。通过设置
rc('verbose',level='debug-hasting',fileo=sys.stdout)
我得到这个输出:
MovieWriter--Command stdout:“MovieWriter--Command stderr:”流映射:\n Stream#0.0->#0.0\n按ctrl-c停止编码\n帧=0 fps=0 q=0.0 Lsize=0kB时间=1000000000.00比特率=0.0kB位/s\r\n视频:0kB音频:0kB全局头:0kB多路复用开销647.368421%
I怀疑您在多重处理中需要联接。您可能是对的,但由于某些原因,
queue.join()
会无限期地暂停程序。
$ vlc foo.mp4 
VLC media player 2.0.8 Twoflower (revision 2.0.8a-0-g68cf50b)
[0xf69108] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[0x7f37fcc01ac8] mp4 demux error: cannot find any /moov/trak
[0x7f37fcc01ac8] es demux error: cannot peek
...
[0x7f37fcc01ac8] ps demux error: cannot peek
[0x7f37fcc01ac8] mpgv demux error: cannot peek
[0x7f37fcc01ac8] mjpeg demux error: cannot peek
[0x7f37fcc01ac8] ps demux error: cannot peek
[0x7f3824000b78] main input error: no suitable demux module for `file/://.../foo.mp4'
...