A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20070908/5d3a01f6/attachment.htm below:

On 08/09/2007, <b class="gmail_sendername">Bill Janssen</b> &lt;<a href="mailto:janssen@parc.com">janssen@parc.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&gt; Be convoluted yourself and do this:<br>&gt;<br>&gt; #define PySSL_BEGIN_ALLOW_THREADS { if (_ssl_locks) { Py_BEGIN_ALLOW_THREADS<br>&gt; #define PySSL_END_ALLOW_THREADS Py_END_ALLOW_THREADS } }<br>&gt;<br>&gt; (Untested, but I think it should work.)
<br><br>Yes, that had occurred to me.&nbsp;&nbsp;We want the code inside the braces<br>still to run if the locks aren&#39;t held, so something more like<br><br>&nbsp;&nbsp;#define PySSL_BEGIN_ALLOW_THREADS { \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PyThreadState *_save;&nbsp;&nbsp;\
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (_ssl_locks_count&gt;0) {_save = PyEval_SaveThread();}<br>&nbsp;&nbsp;#define PySSL_BLOCK_THREADS&nbsp;&nbsp; if (_ssl_locks_count&gt;0){PyEval_RestoreThread(_save)};<br>&nbsp;&nbsp;#define PySSL_UNBLOCK_THREADS if (_ssl_locks_count&gt;0){_save = PyEval_SaveThread()};
<br>&nbsp;&nbsp;#define PySSL_END_ALLOW_THREADS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_ssl_locks_count&gt;0){PyEval_RestoreThread(_save);} \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>would do the trick.&nbsp;&nbsp;Unfortunately, this doesn&#39;t deal with the macro<br>behaviour.&nbsp;&nbsp;The user has &quot;turned on&quot; threading; they expect reads and
<br>writes to yield the GIL so that other threads can make progress.&nbsp;&nbsp;But<br>the fact that threading has been &quot;turned on&quot; after the SSL module has<br>been initialized, means that threads don&#39;t work inside the SSL code.
<br>So the user&#39;s understanding of the system will be broken.<br><br>No, I don&#39;t see any good way to fix this except to add a callback<br>chain inside PyThread_init_thread, which is run down when threads are<br>initialized.&nbsp;&nbsp;Any module which needs to set up threads registers itself
<br>on that chain, and gets called as part of PyThread_init_thread.&nbsp;&nbsp;But<br>I&#39;m far from the smartest person on this list :-), so perhaps someone<br>else will see a good solution.</blockquote><div><br>I think this is a helpful additional tool to solve threading problems.&nbsp; Doesn&#39;t solve everything, but it certainly helps :-)
<br><br>For instance, one thing it doesn&#39;t solve is when a library being wrapped can be initialized with multithreading support, but only allows such initialization as a very first API call; you can&#39;t initialize threading at any arbitrary time during application runtime.&nbsp; Unfortunately I don&#39;t think there is any sane way to fix this problem :-(
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">This has got to be a problem with other extension modules linked to<br>libraries which have their own threading abstractions.
</blockquote><div><br>Yes.<br><br>Another problem is that python extensions  may not wish to incur performance penalty of python threading calls.&nbsp; For instance, pyorbit has these macros:<br><br>#define pyorbit_gil_state_ensure() (PyEval_ThreadsInitialized()? (PyGILState_Ensure()) : 0)
<br><br>#define pyorbit_gil_state_release(state) G_STMT_START { \<br>&nbsp;&nbsp;&nbsp; if (PyEval_ThreadsInitialized())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyGILState_Release(state);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp; } G_STMT_END<br><br>#define pyorbit_begin_allow_threads&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
<br>&nbsp;&nbsp;&nbsp; G_STMT_START {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyThreadState *_save = NULL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (PyEval_ThreadsInitialized())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _save = PyEval_SaveThread();<br><br>#define pyorbit_end_allow_threads&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (PyEval_ThreadsInitialized())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyEval_RestoreThread(_save);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br>&nbsp;&nbsp;&nbsp; } G_STMT_END<br><br>They all call PyEval_ThreadsInitialized() before doing anything thread related to save some performance.&nbsp; The other reason to do it this way is that the Python API calls themselves abort if they are called with threading not initialized.&nbsp; It would be nice the upstream python GIL macros were more like pyorbit and became no-ops when threading is not enabled.
<br><br></div></div>-- <br>Gustavo J. A. M. Carneiro<br>INESC Porto, Telecommunications and Multimedia Unit<br>&quot;The universe is always one step beyond logic.&quot; -- Frank Herbert

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4