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 onBoost.Python
for no good reason. Ideally, onlypython_exposure.cpp
should depend onBoost.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 tomyfunction
. The problem with this method is thatwrap
needs to have the same signature asmyfunction
which would pretty much mean re-implementingBoost.Function
...I would be very thankful if someone had any suggestion to this problem.
原文:https://stackoverflow.com/questions/18618333
最满意答案
好的,纯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 containerdiv#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()
usetranslateY()
.(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 hasz-index:-1
will not disappear behind thebody
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 ...
-
如何在单个视图中处理多个分段控制的事件(How to handle events of multiple segmented control in a single view)[2022-02-12]
将每个分段控件的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 ...
-
分段控制和变量问题(Segmented control and variable issue)[2024-02-02]
似乎AdSettingsTVC的server属性是私有的,因为您已在类别AdSettingsTVC()中的.m文件中定义它。 因此,任何其他文件都无法访问它。 尝试在.h文件中将其公开为 @interface AdSettingsTVC:UIViewController @property (strong, nonatomic) NSString *server; 另外,请注意strong财产 Seems like the server property of AdSettingsTVC is priv ... -
使用分段控制Swift 3(Use Segmented Control Swift 3)[2022-03-10]
您需要通过@IBOutlet将其连接到文件。 然后你可以使用它的索引来做任何你想做的事情。 @IBOutlet var segmentedControl: UISegmentedControl! @IBAction func calculateButton(_ sender: Any) { let index = segmentedControl.selectedSegmentIndex //... } You need to connect it via @IBOutlet to fi ... -
控制3分段(control 3 Segmented)[2023-05-29]
以及我正在尝试这个代码现在的工作:) 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() ...