Maison >développement back-end >Tutoriel Python >不同的语言中多进程和多线程具体的原理是什么?
python中由于全局锁(GIL)的存在导致多线程并不能利用多核,看了一些资料说,java好像对多线程的处理是可以利用多核的硬件资源的(因为java直接调用的操作系统的多线程接口处理的)。不同的语言对多线程的处理是否能做到利用硬件,主要取决于编译器或者解释器对线程的包装吗?还有python中多进程是怎样一个概念,不是说一个程序只有一个进程,进程里面可以有多个线程,那么python是怎么在一个程序中用多个进程的?
Python中由于全局锁(GIL)的存在导致多线程并不能利用多核,看了一些资料说,Java好像对多线程的处理是可以利用多核的硬件资源的(因为 Java直接调用的操作系统的多线程接口处理的)。不同的语言对多线程的处理是否能做到利用硬件,主要取决于编译器或者解释器对线程的包装吗?
<code class="language-c"><span class="kt">void</span>
<span class="nf">PyEval_AcquireLock</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">PyThread_acquire_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">PyEval_ReleaseLock</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">PyThread_release_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">PyEval_AcquireThread</span><span class="p">(</span><span class="n">PyThreadState</span> <span class="o">*</span><span class="n">tstate</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">tstate</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
<span class="n">Py_FatalError</span><span class="p">(</span><span class="s">"PyEval_AcquireThread: NULL new thread state"</span><span class="p">);</span>
<span class="cm">/* Check someone has called PyEval_InitThreads() to create the lock */</span>
<span class="n">assert</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">);</span>
<span class="n">PyThread_acquire_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">PyThreadState_Swap</span><span class="p">(</span><span class="n">tstate</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
<span class="n">Py_FatalError</span><span class="p">(</span>
<span class="s">"PyEval_AcquireThread: non-NULL old thread state"</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">PyEval_ReleaseThread</span><span class="p">(</span><span class="n">PyThreadState</span> <span class="o">*</span><span class="n">tstate</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">tstate</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
<span class="n">Py_FatalError</span><span class="p">(</span><span class="s">"PyEval_ReleaseThread: NULL thread state"</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">PyThreadState_Swap</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="o">!=</span> <span class="n">tstate</span><span class="p">)</span>
<span class="n">Py_FatalError</span><span class="p">(</span><span class="s">"PyEval_ReleaseThread: wrong thread state"</span><span class="p">);</span>
<span class="n">PyThread_release_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">);</span>
<span class="p">}</span>
</code>
GIL这个东西,跟实现有关。java的每一个对象都继承了object类的六个方法,其中包括wait和notify.如果cpython也这么干,就没gil了。实际上,鸟叔曾搞出过这样的python版本,但是由于单线程性能急剧下降,大家不乐意又换成Gil版本了。
多线程难道不是等io用么→_→