The threading library works very well despite the presence of the GIL.
Before I explain, you should know that Python's threads are real threads - they are normal operating system threads running the Python interpreter. The GIL (or Global Interpreter Lock) is only taken when running pure Python code, and in many cases is completely released and not even checked.
The GIL does not prevent these operations from running in parallel:
- IO operations, such as sending & receiving network data or reading/writing to a file.
- Heavy builtin CPU bound operations, such as hashing or compressing.
- Some C extension operations, such as numpy calculations.
Any of these (and plenty more) would run perfectly fine in a parallel fashion, and in the majority of the programs these are the heftier parts taking the longest time.
Building an example API in Python that takes astronomical data and calculates trajectories would mean that:
- Processing the input and assembling the network packets would be done in parallel.
- The trajectory calculations should they be in numpy would all be parallel.
- Adding the data to a database would be parallel.
- Returning the data over the network would be parallel.
Basically the GIL won't affect the vast majority of the program runtime.
Moreover, at least for networking, other methodologies are more prevalent these days such as asyncio
which offers cooperative multi-tasking on the same thread, effectively eliminating the downside of thread overload and allowing for considerably more connections to run at the same time. By utilizing that, the GIL is not even relevant.
The GIL can be a problem and make threading useless in programs that are CPU intensive while running pure Python code, such as a simple program calculating Fibonacci's numbers, but in the majority of real world cases, unless you're running an enormously scaled website such as Youtube (which admittedly has encountered problems), the GIL is not a significant concern.
multiprocessing
library is more helpful to what you seem to be looking for, as it can actually spawn processes that harness individual cores. – Retinoscopythreading
library. It seems it's more for code organization. – Kenosis