简体   繁体   中英

matplotlib (Tkinker backend) crashing in multiprocessing.process - how to call XInitThreads

This question relates to many other posts here that have the same error message when bringing up X-windows from within a Process - none seem to have a solution that works - but first the problem (as I found it using ipython 2.6):

I have a number of long processes and it makes sense to run them in parrallel using a seperate process in a single python session. These processes plot graphs using matplotlib.pyplot. However, when pyplot.show() or pyplot.figure() is called the process crashes the whole python session with the following error:

[xcb] Unknown sequence number while processing queue
[xcb] Most likely this ia a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python2.7: xcb_io.c:259: poll_for_event: Assertion '!xcb_xlib_threads_sequence_lost' failed.
Aborted 

I was able to reproduce this error in a much more simple form from the ipython command line:

from matplotlib import pyplot
import multiprocessing as mp
def plot():
    pyplot.plot([1,2,3,4])
    pyplot.show() #this is the line it crashes on

# the next two lines are only needed if not iython --pyplot or you have not called pyplot.ion()
pyplot.plot([1,2,3,4]) 
pyplot.show() #close plot

p=mp.process.Process(target=plot)
p.start() #crash!!!!!

A check of matplotlib.get_backend() shows I'm running TKAgg / Tkinker Others with the same errors were also using Tkinker to make nice a gui. It is also worth noting that I can run as many of my plot routines from seperate ipython sessions in different xterms with no problem. (Which given the Process command should create it's own namespace would seem to be the same thing).

So my question is, where (in python) do I find XIitThreads? How/when is it run? and fingers crossed it solves my problem (and some similar ones here). I did find this post : Threading: PyQt crashes with "unknown request in queue while dequeuing" but this was for a PyQt4 backend (which I do not have and cannot install.)

EDIT - One warning - if you replace pyplot.show() with pyplot.show(block=False) then you will be left with a window that will not go away - could be an advantage of pyplot.save().

Also all attempts to find calls to xcb_io within Tkinter or matplotlib have failed - anyone know where those are?

You may be happier if you replace .show() with pyplot.save('foo.png') , avoiding the attempt to pop up a new window. Then use a web browser to display *.png.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM