deciding among subprocess, multiprocessing, and thread in Python?

后端 未结 5 831
感情败类
感情败类 2020-11-29 15:10

I\'d like to parallelize my Python program so that it can make use of multiple processors on the machine that it runs on. My parallelization is very simple, in that all the

5条回答
  •  春和景丽
    2020-11-29 15:38

    multiprocessing is a great Swiss-army knife type of module. It is more general than threads, as you can even perform remote computations. This is therefore the module I would suggest you use.

    The subprocess module would also allow you to launch multiple processes, but I found it to be less convenient to use than the new multiprocessing module.

    Threads are notoriously subtle, and, with CPython, you are often limited to one core, with them (even though, as noted in one of the comments, the Global Interpreter Lock (GIL) can be released in C code called from Python code).

    I believe that most of the functions of the three modules you cite can be used in a platform-independent way. On the portability side, note that multiprocessing only comes in standard since Python 2.6 (a version for some older versions of Python does exist, though). But it's a great module!

提交回复
热议问题