Threads

Python has another local parallelization library—thread. The thread objects it provides are created and used in much the same way that multiprocessing.Process objects are, but thread-based processes run in the same memory space as the parent process, while Process objects, when they are started, actually create a new Python interpreter instance (with some connection capabilities to the parent Python interpreter).

Because threads run in the same interpreter and memory space, they are not capable of accessing multiple processors the same way that a Process can.

A thread's access to multiple CPUs on a machine is a function of the Python interpreter that's used to run the code. The standard interpreter that ships with Python (Cpython) and the alternative PyPy interpreter both share this limitation. IronPython, an interpreter that runs under/in the .NET framework, and Jython, which runs in a Java runtime environment, do not have that limitation.

Thread-based parallelization is also far more likely to encounter conflicts with Python's global interpreter lock (GIL). The GIL actively prevents multiple threads from executing or altering the same Python bytecode at the same time. There are some potentially long-running processes that happen outside the GIL's control—I/O, networking, some image processing functionality, and various libraries such as NumPy—but outside those exceptions, any multithreaded Python program that spends a lot of its execution time interpreting or manipulating Python bytecode will eventually hit a GIL bottleneck, losing its parallelization in the process.

More information about the GIL, why it exists, what it does, and so on, can be found on the Python wiki at https://wiki.python.org/moin/GlobalInterpreterLock.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.154.18