Python supports the creation of new threads for [bound or unbound] functions. They can help with multiprocessing. If you want a server to begin certain operations in parallel with others, you may want to use new processes as opposed to new threads. Both threads and processes can provide the same parallel processing benefit. What are some disadvantages of using a thread to call a function?
- Threads cannot invoke an entirely different program. Starting a new process can execute a separate Python program. Threads are limited to calling functions. (These three preceding sentences were paraphrased from page 187 of Programming Python by Mark Lutz.)
- Different threads have access to the same memory addresses of the process (page 186 of Programming Python by Mark Lutz). Shared memory space may be accessible to two or more threads. Thus one thread can make changes that the other thread relies on. If you have variables that need to be globally available and you have multiple different threads, the complexity could be unwieldy with many different functions or methods modifying values in memory producing unpredictable results. You must synchronize queues and threads to ensure thread safety (taken from page 187 of Programming Python by Mark Lutz). By protecting shared data (e.g., with locks), you can mitigate race hazards. Critical race conditions with undesirable outcomes can be difficult to debug (according to page 505 of Expert Python Programming, Third Edition).
- The GIL permits no more than one thread to run via Python (taken from page 187 of Programming Python by Mark Lutz). Python cannot do multi-threading as far as we know (as of January 2019).* (Python threads can make it appear that non-blocking operations are happening concurrently for the user.) The global interpreter lock necessitates you use the C extensions if you want to do true multi-threading. This means that you cannot leverage multi-CPU servers with Python with threads.*
* If you use a search engine for the phrase "python multi-cpu server" and see for yourself the results will pertain to multi-processing.
For something analogous but not in Python, in the Java programming language the keyword "lock" can help you (to learn more see this external site).