{"id":311677,"date":"2020-10-19T15:00:15","date_gmt":"2020-10-19T15:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=311677"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=311677","title":{"rendered":"\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f GIL \u0432 CPython"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/523944\/\">\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043a\u0430\u043a \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u043f\u043e\u0442\u043e\u043a\u0438, \u043a\u043e\u0433\u0434\u0430 \u0431\u043e\u0440\u044e\u0442\u0441\u044f \u0437\u0430 GIL, \u0438\u043b\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 <a href=\"http:\/\/www.dabeaz.com\/python\/GIL.pdf\" rel=\"nofollow\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a> \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f Python3.<\/p>\n<p>  \u0421\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>Ubuntu 16.04<\/code> c \u044f\u0434\u0440\u043e\u043c <code>4.15.0-115-generic<\/code>, \u043d\u0430 \u043c\u0430\u0448\u0438\u043d\u0435 \u0441\u0442\u043e\u0438\u0442 4-\u0445 \u044f\u0434\u0435\u0440\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 <code>Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz<\/code> \u0441 4 GB RAM.<\/p>\n<h2>\u0422\u0435\u043e\u0440\u0438\u044f<\/h2>\n<p>  \u041d\u0438 \u0434\u043b\u044f \u043a\u043e\u0433\u043e \u043d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e \u0432 Linux \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 POSIX threads. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 CPython \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043c\u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439.<\/p>\n<p>  GIL \u0432 Python3 \u044d\u0442\u043e \u0431\u0443\u043b\u0435\u0432\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <code>locked<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u0449\u0438\u0449\u0435\u043d \u043c\u044c\u044e\u0442\u0435\u043a\u0441\u043e\u043c <code>mutex<\/code>, \u0438 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432 <code>false<\/code>, \u041e\u0421 \u00ab\u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u00bb \u043a\u0430\u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>cond<\/code>.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/h4>\n<p>  \u0412 \u0433\u043b\u0430\u0432\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435 (\u0441\u043c. \u0444\u0430\u0439\u043b <code>ceval.c<\/code>) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>eval_frame_handle_pending<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439, \u0435\u0441\u043b\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430 \u043f\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <code>gil_drop_request<\/code>, \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442 GIL, \u0434\u0430\u0432\u0430\u044f \u0448\u0430\u043d\u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0435\u0433\u043e \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">\/* GIL drop request *\/ if (_Py_atomic_load_relaxed(&amp;ceval2-&gt;gil_drop_request)) {     \/* Give another thread a chance *\/     if (_PyThreadState_Swap(&amp;runtime-&gt;gilstate, NULL) != tstate) {         Py_FatalError(&quot;tstate mix-up&quot;);     }     drop_gil(ceval, ceval2, tstate);      \/* Other threads may run now *\/      take_gil(tstate);      if (_PyThreadState_Swap(&amp;runtime-&gt;gilstate, tstate) != NULL) {         Py_FatalError(&quot;orphan tstate&quot;);     } } <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <code>gil_drop_request<\/code> \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>take_gil<\/code> (\u0441\u043c. \u0444\u0430\u0439\u043b <code>ceval_gil.h<\/code>). \u041e\u043d\u0430 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u043c <code>interval<\/code> \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434 \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>cond<\/code>. \u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u0451\u043c \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u043a \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0434\u043e\u043b\u044c\u0448\u0435. <b>\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>gil_drop_request<\/code>, \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a (\u043a\u0440\u043e\u043c\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e) \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435.<\/b><\/p>\n<pre><code class=\"cpp\">while (_Py_atomic_load_relaxed(&amp;gil-&gt;locked)) {     unsigned long saved_switchnum = gil-&gt;switch_number;      unsigned long interval = (gil-&gt;interval &gt;= 1 ? gil-&gt;interval : 1);     int timed_out = 0;     COND_TIMED_WAIT(gil-&gt;cond, gil-&gt;mutex, interval, timed_out);      \/* If we timed out and no switch occurred in the meantime, it is time         to ask the GIL-holding thread to drop it. *\/     if (timed_out &amp;&amp;         _Py_atomic_load_relaxed(&amp;gil-&gt;locked) &amp;&amp;         gil-&gt;switch_number == saved_switchnum)     {          if (tstate_must_exit(tstate)) {                MUTEX_UNLOCK(gil-&gt;mutex);                PyThread_exit_thread();          }          assert(is_tstate_valid(tstate));           SET_GIL_DROP_REQUEST(interp);      } } <\/code><\/pre>\n<p>  \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>drop_gil<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>locked<\/code> \u0432 <code>false<\/code>, \u00ab\u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f\u00bb \u0443\u0441\u043b\u043e\u0432\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <code>cond<\/code>.<\/p>\n<pre><code class=\"cpp\">MUTEX_LOCK(gil-&gt;mutex); _Py_ANNOTATE_RWLOCK_RELEASED(&amp;gil-&gt;locked, \/*is_write=*\/1); _Py_atomic_store_relaxed(&amp;gil-&gt;locked, 0); COND_SIGNAL(gil-&gt;cond); MUTEX_UNLOCK(gil-&gt;mutex); <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>interval<\/code>, \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>sys.setswitchinterval<\/code>. \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e 5 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u043c (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>sys.getswitchinterval<\/code>).<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u043f\u043e\u0442\u043e\u043a \u043f\u0438\u0448\u0435\u0442 \u0432 \u0444\u0430\u0439\u043b \u0438\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0441\u0435\u0442\u044c\u044e (\u0438\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0435\u0449\u0451 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e I\/O \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438), \u0442\u043e \u0432 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 GIL \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f. \u0422\u0430\u043a \u0436\u0435 \u043e\u043d \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a Numpy.<\/p>\n<h2>\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<h4>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>take_gil<\/code> \u0438 <code>drop_gil<\/code>.<\/p>\n<pre><code class=\"cpp\">static void take_gil(PyThreadState *tstate) {     ...     while (_Py_atomic_load_relaxed(&amp;gil-&gt;locked)) {         unsigned long saved_switchnum = gil-&gt;switch_number;          unsigned long interval = (gil-&gt;interval &gt;= 1 ? gil-&gt;interval : 1);         int timed_out = 0;         COND_TIMED_WAIT(gil-&gt;cond, gil-&gt;mutex, interval, timed_out);          fprintf(stdout, &quot;busy gil: %d %d %d\\n&quot;, pthread_self(), ATOMIC_COUNT, interval);         ...     }     ...     fprintf(stdout, &quot;take gil: %d %d\\n&quot;, pthread_self(), ATOMIC_COUNT); }  static void drop_gil(struct _ceval_runtime_state *ceval, struct _ceval_state *ceval2,          PyThreadState *tstate) {      ...      fprintf(stdout, &quot;drop gil: %d %d\\n&quot;, pthread_self(), ATOMIC_COUNT); } <\/code><\/pre>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432 \u0444\u0430\u0439\u043b \u0432 \u043a\u043e\u043d\u0446\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043b\u043e\u0433\u0438 \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u044b\u0432\u043e\u0434\u0430 \u0441\u0440\u0430\u0437\u0443 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u041f\u043e \u043e\u0441\u0438 \u0430\u0431\u0441\u0446\u0438\u0441\u0441 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0438\u043a\u0438, \u0430 \u043d\u0435 \u0432\u0440\u0435\u043c\u044f. \u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u0432 \u043a\u0430\u043a\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0442\u043e\u043a\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0445 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u041e\u0421. <\/p>\n<p>  \u0417\u0435\u043b\u0451\u043d\u044b\u0435 \u043f\u043e\u043b\u043e\u0441\u043a\u0438 \u2014 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u0432 \u0442\u0438\u043a\u0430\u0445) \u043e\u0442 \u0442\u0438\u043a\u0430, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0442\u043e\u043a \u0432\u0437\u044f\u043b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u0434\u043e \u0442\u0438\u043a\u0430, \u043a\u043e\u0433\u0434\u0430 \u2014 \u043e\u0442\u0434\u0430\u043b. \u041a\u0440\u0430\u0441\u043d\u044b\u0435 \u043f\u043e\u043b\u043e\u0441\u043a\u0438 \u2014 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u0442\u0438\u043a\u0430, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0442\u043e\u043a \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b <code>gil_drop_request<\/code>, \u0434\u043e \u0442\u0438\u043a\u0430, \u043a\u043e\u0433\u0434\u0430 \u043b\u0438\u0431\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b \u044d\u0442\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u043b\u0438\u0431\u043e \u0432\u0437\u044f\u043b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u0431\u043e\u0440\u043a\u0438<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"bash\">git clone https:\/\/github.com\/python\/cpython.git mkdir debug_python cd debug_python ..\/cpython\/configure make cd .. <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440\u044b<\/h4>\n<p>  <\/p>\n<ol>\n<li>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u043e\u043b\u0433\u043e.\n<p>  \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c:  <\/p>\n<pre><code class=\"python\">debug_python\/python main.py --type=&quot;cpu-bound&quot; 1&gt;logs python drawing.py <\/code><\/pre>\n<p>  \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 \u043d\u0438\u0436\u0435 \u0432 \u0443\u0437\u043a\u0438\u0445 \u043f\u043e\u043b\u043e\u0441\u043a\u0430\u0445 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0435 5 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0442\u0438\u043a (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430).<br \/>   <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/p8\/sj\/j3\/p8sjj3gqkmf1vof0l97dgnuwvsy.png\" alt=\"image\">   <\/li>\n<li>\u041d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a, \u0447\u0442\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b <code>gil_drop_request<\/code>, \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435.\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e.<\/p>\n<p>  \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0436\u0434\u0451\u0442 4 \u0440\u0430\u0437\u0430 \u043f\u043e \u043e\u043a\u043e\u043b\u043e 5 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435.<br \/>   <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/f8\/e3\/d5\/f8e3d5_67wqjvqvosgrl5dyg2ru.png\" alt=\"image\">   <\/li>\n<li>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>interval<\/code> \u0432 30 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434.\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/k-\/o0\/9s\/k-o09s7qcvvsrtb3fhq9bghnh-a.png\" alt=\"image\">   <\/li>\n<\/ol>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041a\u043e\u0434 \u0438\u0437 main.py<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">import sys import threading import random import argparse   text = &quot;&quot;&quot; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque id mi tortor. Pellentesque habitant morbi  tristique senectus et netus et malesuada fames ac turpis egestas. Mauris arcu neque, tempor interdum magna non,  fringilla maximus ex. Proin a mollis elit. Nunc lacinia mollis sem, eget sodales ligula vulputate at. In euismod  elit vel mi suscipit, in pellentesque velit tempor. Nullam eleifend ornare risus ac ultricies. Nam interdum velit  sit amet eros dapibus euismod. Proin non orci imperdiet, interdum velit in, cursus justo. Nullam fringilla, tortor  quis sollicitudin pretium, erat felis porta odio, dictum sodales massa nisi id magna. Integer vitae ipsum ac lectus  imperdiet tristique ac a nibh. Interdum et malesuada fames ac ante ipsum primis in faucibus. Maecenas suscipit id mi  ac eleifend. Interdum et malesuada fames ac ante ipsum primis in faucibus. &quot;&quot;&quot;   def func_cpu_bound(n):     for _ in range(n):          [random.randint(1, 1000000) for _ in range(10000)].sort()   def func_io_bound(n):     while n &gt; 0:         with open(f&quot;{threading.get_ident()}&quot;, &quot;w&quot;) as f:             f.write(text)         n -= 1   def run_threads(type):     if type == &quot;cpu-bound&quot;:         t1 = threading.Thread(target=func_cpu_bound, args=(10000000,))         t2 = threading.Thread(target=func_cpu_bound, args=(10000000,))     elif type == &quot;io-bound&quot;:         t1 = threading.Thread(target=func_io_bound, args=(50,))         t2 = threading.Thread(target=func_io_bound, args=(50,))      t1.start()     t2.start()     t1.join()     t2.join()   def run_without_threads(type):     if type == &quot;cpu-bound&quot;:         func_cpu_bound(10000000)         func_cpu_bound(10000000)     elif type == &quot;io-bound&quot;:         func_io_bound(50)         func_io_bound(50)   def parse_args():     parser = argparse.ArgumentParser()     parser.add_argument(&quot;-wt&quot;, &quot;--without-threads&quot;, dest=&quot;without_threads&quot;, action=&quot;store_true&quot;)     parser.add_argument(&quot;-t&quot;, &quot;--type&quot;, dest=&quot;type&quot;, choices=[&quot;cpu-bound&quot;, &quot;io-bound&quot;])     args = parser.parse_args()     return args   def main():     args = parse_args()      if args.without_threads:         run_without_threads(args.type)     else:         run_threads(args.type)   if __name__ == &quot;__main__&quot;:     main() <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u041a\u043e\u0434 \u0438\u0437 drawing.py<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">from collections import defaultdict  import matplotlib.pyplot as plt import matplotlib.patches as patches   with open(&quot;logs&quot;, &quot;r&quot;) as f:     fig, ax = plt.subplots(1, figsize=(20, 10))      lines = defaultdict(list)     for line in f:          if &quot;:&quot; not in line:              continue          name, tokens = line.split(&quot;:&quot;)          name = name.strip()          if &quot;gil&quot; in name:              ident, num_op, *other = tokens.strip().split()              ident = int(ident)              num_op = int(num_op)              lines[ident].append((num_op, name))       def get_color(name):         if name == &quot;take gil&quot;:              return &quot;g&quot;         elif name == &quot;busy gil&quot;:              return &quot;r&quot;         elif name == &quot;drop gil&quot;:              return &quot;y&quot;      for idx, (key, items) in enumerate(lines.items()):          for i in range(len(items)):              if items[i][1] in [&quot;take gil&quot;, &quot;busy gil&quot;]:                  if i + 1 &lt; len(items):                      rect = patches.Rectangle((items[i][0], idx), items[i + 1][0] - items[i][0], 1, color=get_color(items[i][1]), fill=True)                       ax.add_patch(rect)                  else:                      rect = patches.Rectangle((items[i][0], idx), num_op - items[i][0], 1, color=get_color(items[i][1]), fill=True)                      ax.add_patch(rect)     plt.xlim(9950, num_op)     plt.ylim(0, 3)     plt.show() <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432\u0441\u0435 \u043e\u0442\u0440\u0435\u0437\u043a\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0432\u043d\u044b, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u044d\u0442\u0438 \u043e\u0442\u0440\u0435\u0437\u043a\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043f\u043e 5 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434. \u0418\u0437 \u0447\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442, \u0447\u0442\u043e \u0432 Python3 \u043d\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u043d\u0430\u0434\u043e\u043b\u0433\u043e \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u0432 Python2. \u0418 \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u0441 \u00ab\u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438\u00bb \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u043c\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f\u043c\u0438, \u0432 \u043e\u0431\u0449\u0435\u043c, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u00ab\u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e\u00bb \u0441\u043d\u043e\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043a\u0432\u0430\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0412\u044b\u0445\u043e\u0434\u0438\u0442, \u0447\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0445\u043e\u0442\u044c \u0438 \u043d\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0435, \u043d\u043e \u0432\u0441\u0451 \u0436\u0435.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/523944\/\"> https:\/\/habr.com\/ru\/post\/523944\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/523944\/\">\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043a\u0430\u043a \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u043f\u043e\u0442\u043e\u043a\u0438, \u043a\u043e\u0433\u0434\u0430 \u0431\u043e\u0440\u044e\u0442\u0441\u044f \u0437\u0430 GIL, \u0438\u043b\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 <a href=\"http:\/\/www.dabeaz.com\/python\/GIL.pdf\" rel=\"nofollow\">\u043e\u0442\u0441\u044e\u0434\u0430<\/a> \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f Python3.<\/p>\n<p>  \u0421\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>Ubuntu 16.04<\/code> c \u044f\u0434\u0440\u043e\u043c <code>4.15.0-115-generic<\/code>, \u043d\u0430 \u043c\u0430\u0448\u0438\u043d\u0435 \u0441\u0442\u043e\u0438\u0442 4-\u0445 \u044f\u0434\u0435\u0440\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 <code>Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz<\/code> \u0441 4 GB RAM.<\/p>\n<h2>\u0422\u0435\u043e\u0440\u0438\u044f<\/h2>\n<p>  \u041d\u0438 \u0434\u043b\u044f \u043a\u043e\u0433\u043e \u043d\u0435 \u0441\u0435\u043a\u0440\u0435\u0442, \u0447\u0442\u043e \u0432 Linux \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 POSIX threads. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 CPython \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043c\u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439.<\/p>\n<p>  GIL \u0432 Python3 \u044d\u0442\u043e \u0431\u0443\u043b\u0435\u0432\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <code>locked<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u0449\u0438\u0449\u0435\u043d \u043c\u044c\u044e\u0442\u0435\u043a\u0441\u043e\u043c <code>mutex<\/code>, \u0438 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432 <code>false<\/code>, \u041e\u0421 \u00ab\u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u00bb \u043a\u0430\u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u0442\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>cond<\/code>.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-311677","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=311677"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311677\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}