7

I am trying to make a python script that uses the multiprocessing module to produce 2 (or more) GTK windows. I am hitting a wall here it seems. Here is the code and the errors I am getting:

p1 = Process(target=tiny_gtk_process, name="process 1")
p1.start()
p2 = Process(target=tiny_gtk_process, name="process 2")
p2.start()

and:

def tiny_gtk_process():

    import gtk

    window = gtk.Window()
    window.set_size_request(800,600)

    window.show_all()

    gtk.main()

Most of the time I am getting:

multiwin.py: Fatal IO error 0 (Success) on X server :0.0. python: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) - (long) (dpy->request)) <= 0)' failed.

Sometimes I get:

multiwin.py: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.

Is the issue the gtk loop? Doesn't multiprocessing isolate them?

Any ideas would be very helpful.

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
  • Do you really want to use multiprocess, or do you just want to have 2 windows ? – liberforce Apr 03 '12 at 12:56
  • I really need this to be in separate processes. – Stratos Goudelis Apr 03 '12 at 13:11
  • Are you importing gtk in the calling script? I was able to reproduce your problem when I had an import gtk in the calling script, but not when I removed it and the import is only in the tiny_gtk_process function. – bohrax May 26 '12 at 19:24

1 Answers1

3

The problem is most likely that you are using multiprocessing.Process, which creates new processes using fork() instead of exec(). This means that each sub-process shares the same file handles as its parent, including those that connect it to the X server. The crash is caused because multiple processes are trying to communicate with the server simultaneously over the same connection.

A better solution would be to use subprocess.Popen or similar to start your processes. If you want multiprocessing-like communication between your processes, see my answer to this question.

Community
  • 1
  • 1
Luke
  • 11,374
  • 2
  • 48
  • 61