In python, due to the existence of a global lock (GIL), multiple threads cannot use multiple cores. According to some documents, java seems to be able to use multi-threaded hardware resources (Because java directly calls the multi-threaded interface of the operating system for processing ). Can I use hardware for multi-thread processing in different languages depends mainly on the packaging of threads by the compiler or interpreter? There is also the concept of multi-process in python. It doesn't mean that a program has only one process and there can be multiple threads in the process. How does python implement global lock (GIL) in a program) as a result, multiple threads cannot use multiple cores, java seems to be able to use multi-threaded hardware resources (Because java directly calls the multi-threaded interface of the operating system for processing ). Can I use hardware for multi-thread processing in different languages depends mainly on the packaging of threads by the compiler or interpreter? What is the concept of multi-process in python? It doesn't mean that a program has only one process and there can be multiple threads in the process. How does python use multiple processes in a program? Reply content:
In Python, due to the existence of a global lock (GIL), multiple threads cannot use multiple cores. According to some documents, java seems to be able to use multi-threaded hardware resources (Because Java directly calls the multi-threaded interface of the operating system for processing ). Can I use hardware for multi-thread processing in different languages depends mainly on the packaging of threads by the compiler or interpreter?
I feel that the phrase "multi-threaded interface processing for the operating system directly called by Java" in your brackets does not seem to be the reason why Python and Java differ in multithreading, go to the Python source code Thread. h and a related header file will find that the Python thread calls pthread_create () on Linux and CreateThread () on Windows, all of which are rooted in the original thread.
What actually leads to the difference is actually the GIL you mentioned at the beginning. It has nothing to do with "directly calling the multi-threaded interface of the Operating System. In fact, Python GIL only exists in CPython, Jython does not, and Ruby's MRI implementation also has GIL, while JRuby does not.
The ceval. c file in Python source code has a GIL click on the site, for example, lines 256th to 290th are as follows:
voidPyEval_AcquireLock(void){ PyThread_acquire_lock(interpreter_lock, 1);}voidPyEval_ReleaseLock(void){ PyThread_release_lock(interpreter_lock);}voidPyEval_AcquireThread(PyThreadState *tstate){ if (tstate == NULL) Py_FatalError("PyEval_AcquireThread: NULL new thread state"); /* Check someone has called PyEval_InitThreads() to create the lock */ assert(interpreter_lock); PyThread_acquire_lock(interpreter_lock, 1); if (PyThreadState_Swap(tstate) != NULL) Py_FatalError( "PyEval_AcquireThread: non-NULL old thread state");}voidPyEval_ReleaseThread(PyThreadState *tstate){ if (tstate == NULL) Py_FatalError("PyEval_ReleaseThread: NULL thread state"); if (PyThreadState_Swap(NULL) != tstate) Py_FatalError("PyEval_ReleaseThread: wrong thread state"); PyThread_release_lock(interpreter_lock);}
GIL is related to implementation. Every java object inherits the six methods of the object class, including wait and policy. If cpython does the same thing, there will be no gil. In fact, Uncle bird once developed such a python version, but because of the sharp decline in single-thread performance, we are not happy to switch to the Gil version. Isn't multithreading suitable for I/O? → _ →
This depends on how the implementation is implemented.
Python also has multiple processes (*/ω \ *). The first problem is that python cannot use multiple cores because the Virtual Machine parser is single-threaded during running. (It is estimated that it was designed in the beginning), so the virtual machine can run only one copy of the context at any time and only process one copy of the python VM command. Multithreading is false. GIL locks the running context and switches context to simulate multithreading.
The process is independent of the language and the operating system. Multi-process is a process created using the operating system function, not in the same program. Some languages can call the operating system API, which is the most direct