Embedding Python inside a multithreaded C++ program

论坛 期权论坛     
选择匿名的用户   2021-5-30 01:48   319   0
<h1 class="entry-title" style="border:0px; font-family:inherit; font-size:18px; font-style:inherit; font-weight:inherit; margin:0px 20px; outline:0px; padding:6px 0px; vertical-align:baseline; color:rgb(102,102,102)"> <a href="http://blog.medin.name/2012/02/12/embedding-python-inside-a-multithreaded-c-program/" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(102,102,102); text-decoration:none" title="Permalink to Embedding Python inside a multithreaded C&#43;&#43; program">Embedding Python inside a multithreaded C&#43;&#43; program</a></h1>
<div class="entry-content" style="border:0px; font-size:15px; margin:20px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(102,102,102); line-height:30px">
<p style="border:0px; font-style:inherit; margin-top:0px; margin-bottom:0px; outline:0px; padding-top:0px; padding-bottom:10px; vertical-align:baseline; line-height:25px"> <img align="right" alt="" border="0" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef6e8469d1359e67396b6aaa8043c4.png" style="max-width:100%; padding-left:0px; padding-right:0px; display:inline; float:right; padding-top:0px; border:0px">This is a tutorial for how to embed python correctly inside a multi threaded program. Python is a very neat language which is very easy to embed inside C&#43;&#43; thanks to the boost::python library. But there are some crucial parts which is missing from boost:python in regards to how to manage GIL and thread state which I introduce here. Since this is my first C&#43;&#43; tutorial I will include boost:thread as well as a quick hello world application as well.</p>
<p style="border:0px; font-style:inherit; margin-top:0px; margin-bottom:0px; outline:0px; padding-top:0px; padding-bottom:10px; vertical-align:baseline; line-height:25px"> <span id="more-223" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline"></span></p>
<h2 style="border:0px; font-family:inherit; font-size:22px; font-style:inherit; margin:10px 0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(49,112,201)"> Hello World</h2>
<p style="border:0px; font-style:inherit; margin-top:0px; margin-bottom:0px; outline:0px; padding-top:0px; padding-bottom:10px; vertical-align:baseline; line-height:25px"> First I’d thought I d mention that the full source can be found on <a href="http://github.com/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">github</a><a href="https://github.com/mickem/embed_python/" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)">https://github.com/mickem/embed_python/</a>.</p>
<p style="border:0px; font-style:inherit; margin-top:0px; margin-bottom:0px; outline:0px; padding-top:0px; padding-bottom:10px; vertical-align:baseline; line-height:25px"> Then since this is a project which uses <a href="http://www.cmake.org/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">CMake</a> figured I would start with a simple Hello World in case you are not familiar with <a href="http://www.cmake.org/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">CMake</a>.</p>
<p style="border:0px; font-style:inherit; margin-top:0px; margin-bottom:0px; outline:0px; padding-top:0px; padding-bottom:10px; vertical-align:baseline; line-height:25px"> First of all <a href="http://www.cmake.org/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">CMake</a> is a tool which generates Makefiles similary to automake so the actual build will be performed with normal make. But <a href="http://www.cmake.org/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">CMake</a> has the added benefit of being able to also produce build files for Windows (Visual Studio) as well as other tools and build systems. So for me beeing a windows developer automake is not really an option.</p>
<p style="border:0px; font-style:inherit; margin-top:0px; margin-bottom:0px; outline:0px; padding-top:0px; padding-bottom:10px; vertical-align:baseline; line-height:25px"> The <a href="http://www.cmake.org/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">CMake</a> build information resides in files called CMakeLists.txt which you will find in each directory. Now this post is not about <a href="http://www.cmake.org/" rel="noopener noreferrer" style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:none 0px; padding:0px; vertical-align:baseline; color:rgb(43,145,203)" target="_blank">CMake</a> so I will not go into details about this but to make sure “CMake” works make sure that the project Hello World builds
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP