Why doesn't tkinter play nicely with multiprocessing?

前端 未结 2 1709
说谎
说谎 2020-12-19 09:36

The following code hangs without doing anything in python 3.2.2 in linux:

import tkinter
from multiprocessing import Process

def f():
    root = tkinter.Tk(         


        
2条回答
  •  盖世英雄少女心
    2020-12-19 09:44

    My suspicion is that the problem has to do with the connection to the X server (usually a socket). If this is created before the process is fork()-ed, the child process inherits this connection. But if it tries to use it, the X server gets confused.

    After a cursory look at at Tkinter.py, it looks like maybe calling the NoDefaultRoot function before starting the process might be useful. It all depends on when the connection to the X server is made.

    Otherwise importing Tkinter after the fork seems the way to go.

提交回复
热议问题