首页 \ 问答 \ Boost.Python:封装函数释放GIL(Boost.Python: Wrap functions to release the GIL)

Boost.Python:封装函数释放GIL(Boost.Python: Wrap functions to release the GIL)

我目前正在与Boost.Python合作,并希望得到一些帮助来解决棘手的问题。

上下文

当C ++方法/函数暴露给Python时,它需要释放GIL(全局解释器锁)以让其他线程使用解释器。 这样,当python代码调用C ++函数时,解释器可以被其他线程使用。 现在,每个C ++函数看起来像这样:

// module.cpp
int myfunction(std::string question)
{
    ReleaseGIL unlockGIL;
    return 42;
}

为了传递它来增强python,我这样做:

// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
    def("myfunction", &myfunction);
}

问题

此方案工作正常,但它意味着module.cpp依赖于Boost.Python没有很好的理由。 理想情况下,只有python_exposure.cpp应该依赖于Boost.Python

解?

我的想法是使用Boost.Function来包装函数调用,如下所示:

// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
    def("myfunction", wrap(&myfunction));
}

在调用myfunction过程中,这个wrap会负责解锁GIL。 这种方法的问题是wrap需要与myfunction具有相同的签名,这几乎意味着重新实现Boost.Function ...

如果有人对此问题有任何建议,我将非常感激。


I am currently working with Boost.Python and would like some help to solve a tricky problem.

Context

When a C++ method/function is exposed to Python, it needs to release the GIL (Global Interpreter Lock) to let other threads use the interpreter. This way, when the python code calls a C++ function, the interpreter can be used by other threads. For now, each C++ function looks like this:

// module.cpp
int myfunction(std::string question)
{
    ReleaseGIL unlockGIL;
    return 42;
}

To pass it to boost python, I do:

// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
    def("myfunction", &myfunction);
}

Problem

This scheme works fine, however it implies that module.cpp depends on Boost.Python for no good reason. Ideally, only python_exposure.cpp should depend on Boost.Python.

Solution?

My idea was to play with Boost.Function to wrap the function calls like this:

// python_exposure.cpp
BOOST_PYTHON_MODULE(PythonModule)
{
    def("myfunction", wrap(&myfunction));
}

Here wrap would be in charge of unlocking the GIL during the call to myfunction. The problem with this method is that wrap needs to have the same signature as myfunction which would pretty much mean re-implementing Boost.Function...

I would be very thankful if someone had any suggestion to this problem.


原文:https://stackoverflow.com/questions/18618333
更新时间:2023-01-09 12:01

最满意答案

好的,纯CSS,似乎我回来晚了,还是比不回来, JS Fiddle - 更新 (1)(2)

更新代码z-index值添加到容器div#radios (3)

body {
  background: #EEE url('//www.dailyfreepsd.com/wp-content/uploads/2013/09/underwater-blurred-background.jpg');
  background-size: cover;
}
#radios {
  position: relative;
  background-color: tomato;
  z-index: 5;
  width: 363px;
}
input {
  display: none;
}
#bckgrnd,
.labels {
  width: 120px;
  height: 30px;
  text-align: center;
  display: inline-block;
  padding-top: 10px;
  margin-right: -3px;
  z-index: 2;
  cursor: pointer;
  outline: 1px solid green;
}
#bckgrnd {
  background-color: orange;
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
}
#rad1:checked ~ #bckgrnd {
  transform: translateX(0);
  transition: transform 0.5s ease-in-out;
}
#rad2:checked ~ #bckgrnd {
  transform: translateX(120px);
  transition: transform 0.5s ease-in-out;
}
#rad3:checked ~ #bckgrnd {
  transform: translateX(241px);
  transition: transform 0.5s ease-in-out;
}
<div id="radios">
  <input id="rad1" type="radio" name="radioBtn" checked>
  <label class="labels" for="rad1">First Option</label>
  <input id="rad2" type="radio" name="radioBtn">
  <label class="labels" for="rad2">Second Option</label>
  <input id="rad3" type="radio" name="radioBtn">
  <label class="labels" for="rad3">Third Option</label>
  <div id="bckgrnd"></div>
</div>


编辑:

(1)对于较小的屏幕,如果下面垂直显示这些无线电,则可以使用某个断点进行媒体查询,而不是translateX()使用translateY()

(2)我的下面的解决方案添加一个div <div id="bckgrnd"></div>作为容器#radios div的最后一个孩子,您可以通过javascript / jquery添加,而是添加此jquery: JS小提琴2 - 更新

$(document).ready(function(){
    $('#radios').append('<div id="bckgrnd"></div>');
});

(3) z-index:; 值只是为了确保具有z-index:-1#bckgrnd不会消失在body后面或任何包含#radios div的元素。 所以现在我们可以设置一个背景图像的身体和一个背景颜色到一个容器div而不用担心它。Test JS小提琴


Ok, Pure CSS, seems I came back late, still better than not coming back, JS Fiddle-Updated (1) (2)

Updated Code: added z-index value to the container div#radios (3)

body {
  background: #EEE url('//www.dailyfreepsd.com/wp-content/uploads/2013/09/underwater-blurred-background.jpg');
  background-size: cover;
}
#radios {
  position: relative;
  background-color: tomato;
  z-index: 5;
  width: 363px;
}
input {
  display: none;
}
#bckgrnd,
.labels {
  width: 120px;
  height: 30px;
  text-align: center;
  display: inline-block;
  padding-top: 10px;
  margin-right: -3px;
  z-index: 2;
  cursor: pointer;
  outline: 1px solid green;
}
#bckgrnd {
  background-color: orange;
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
}
#rad1:checked ~ #bckgrnd {
  transform: translateX(0);
  transition: transform 0.5s ease-in-out;
}
#rad2:checked ~ #bckgrnd {
  transform: translateX(120px);
  transition: transform 0.5s ease-in-out;
}
#rad3:checked ~ #bckgrnd {
  transform: translateX(241px);
  transition: transform 0.5s ease-in-out;
}
<div id="radios">
  <input id="rad1" type="radio" name="radioBtn" checked>
  <label class="labels" for="rad1">First Option</label>
  <input id="rad2" type="radio" name="radioBtn">
  <label class="labels" for="rad2">Second Option</label>
  <input id="rad3" type="radio" name="radioBtn">
  <label class="labels" for="rad3">Third Option</label>
  <div id="bckgrnd"></div>
</div>


Edit:

(1) For smaller screens you can make a media query with a certain break point if below show these radios vertically, and instead of translateX() use translateY().

(2) my below solution adds a div <div id="bckgrnd"></div> as the last child of the container #radios div, you can add by javascript/jquery instead, to do so you can add this jquery: JS Fiddle 2-Updated

$(document).ready(function(){
    $('#radios').append('<div id="bckgrnd"></div>');
});

(3) The z-index:; value was added just to ensure that the #bckgrnd - which has z-index:-1 will not disappear behind the body or whatever element contains the #radios div. so now we can set a background image to the body and a background color to a container div without worrying about it.. Test JS Fiddle

相关问答

更多
  • 我们通过使用单个tableview,然后在每个tableview回调方法中执行if / case语句来根据在分段控件中选择哪个值来返回正确的数据。 首先,将segmentedControl添加到titleView,并为其更改时设置一个回调函数: - (void) addSegmentedControl { NSArray * segmentItems = [NSArray arrayWithObjects: @"One", @"Two", nil]; segmentedControl = [ ...
  • 好的,纯CSS,似乎我回来晚了,还是比不回来, JS Fiddle - 更新 (1)(2) 更新代码 :将z-index值添加到容器div#radios (3) body { background: #EEE url('//www.dailyfreepsd.com/wp-content/uploads/2013/09/underwater-blurred-background.jpg'); background-size: cover; } #radios { position: re ...
  • 您可以使用selectedSegment属性。 var selectedSegment: Int 所选段落的索引,如果没有选择段,则为-1。 按钮“+”的索引为0,“ - ”的索引为1。 你可以这样做: switch sender.selectedSegment { case 0: // do actions if "+" case 1: // do actions if "-" default: print("No actions for this button") } You ca ...
  • 您可以简单地将复选框的状态存储在变量中,并根据这些变量隐藏div。 下面包含的第一个变量用于页面加载,其他用于对象更改时。 在这里查看: https : //jsfiddle.net/ezfy66f9/ var checked1 = $('#option-1').is(':checked'); var checked2 = $('#option-2').is(':checked'); var checked3 = $('#option-3').is(':checked'); if (checked1 == ...
  • 它应该是 performSegueWithIdentifier("hello1", sender: self) 代替 shouldPerformSegueWithIdentifier("hello1", sender: self) 所以你的代码应该是这样的: @IBOutlet weak var SegmentControl: UISegmentedControl! @IBAction func Segment(sender: UISegmentedControl) { switch SegmentCo ...
  • 将每个分段控件的tag属性设置为不同的整数。 然后,在您的方法中,您将该值设置为值更改时的操作,使用[sender tag]检查tag属性设置为哪个整数。 Set the tag property on each segmented control to a different integer. Then in your method you set as the action for when the value changes, check which integer the tag property ...
  • 将分段控件添加到nib / Storyboard 将以下代码添加到.h中 @property(nonatomic,retain) IBOutlet UISegmentedControl *Segment; 在storyboard或xib中,确保文件所有者具有与编写插座的类相同的类名 右键单击segmantControl,出现一个带有出口和操作的窗口 单击并拖动引用插座并将其放在文件所有者上,将出现一个新的弹出窗口,其中包括用代码选择的插座选择它。 连接已建立 Add a segmented control ...
  • 似乎AdSettingsTVC的server属性是私有的,因为您已在类别AdSettingsTVC()中的.m文件中定义它。 因此,任何其他文件都无法访问它。 尝试在.h文件中将其公开为 @interface AdSettingsTVC:UIViewController @property (strong, nonatomic) NSString *server; 另外,请注意strong财产 Seems like the server property of AdSettingsTVC is priv ...
  • 您需要通过@IBOutlet将其连接到文件。 然后你可以使用它的索引来做任何你想做的事情。 @IBOutlet var segmentedControl: UISegmentedControl! @IBAction func calculateButton(_ sender: Any) { let index = segmentedControl.selectedSegmentIndex //... } You need to connect it via @IBOutlet to fi ...
  • 以及我正在尝试这个代码现在的工作:) import UIKit class ViewController: UIViewController { @IBOutlet weak var orange: UIView! @IBOutlet weak var yellow: UIView! @IBOutlet weak var red: UIView! override func viewDidLoad() { super.viewDidLoad() ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)