[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Figure 8-35. demoAll_prg: independent programs


Launching GUIs as programs other ways: multiprocessing


If you backtrack to Chapter 5 to study the portable launcher module used by Exam-
ple 8-34 to start programs, you’ll see that it works by using os.spawnv on Windows and
os.fork/exec on others. The net effect is that the GUI processes are effectively started
by launching command lines. These techniques work well, but as we learned in Chap-
ter 5 , they are members of a larger set of program launching tools that also includes
os.popen, os.system, os.startfile, and the subprocess and multiprocessing modules;
these tools can vary subtly in how they handle shell window connections, parent
process exits, and more.


For example, the multiprocessing module we studied in Chapter 5 provides a similarly
portable way to run our GUIs as independent processes, as demonstrated in Exam-
ple 8-35. When run, it produces the exact same windows shown in Figure 8-35, except
that the label in the main window is different.


Example 8-35. PP4E\Gui\Tour\demoAll-prg-multi.py


"""
4 demo classes run as independent program processes: multiprocessing;
multiprocessing allows us to launch named functions with arguments,
but not lambdas, because they are not pickleable on Windows (Chapter 5);
multiprocessing also has its own IPC tools like pipes for communication;
"""


Running GUI Code Three Ways | 479
Free download pdf