简体   繁体   English

使用boost.python的真正多线程

[英]True multithreading with boost.python

I'm trying to test a multi-threaded C++ DLL. 我正在尝试测试多线程C ++ DLL。 This DLL is supposed to be thread-safe. 该DLL应该是线程安全的。 I have it wrapped with boost.python, and I'd like to create multiple python threads to exercise the DLL through the boost.python wrapper. 我用boost.python包裹了它,并且我想创建多个python线程来通过boost.python包装器执行DLL。 I'm actually trying to cause threading problems. 我实际上是在尝试引起线程问题。

What I can't seem to find good documentation on is whether the python interpreter will support two of its threads (on different cores, say) calling into an imported module concurrently, and whether the GIL needs tending at all given that I don't want any added safety above what the DLL is supposed to provide. 我似乎找不到很好的文档,是python解释器是否将同时支持其两个线程(例如,在不同的内核上)调用导入的模块,以及鉴于我不愿意,GIL是否根本需要维护?想要DLL应该提供的安全之外的任何附加安全性。

Can anyone describe or refer me to a description of python calling DLL modules from multiple threads and how the GIL is suppsed to be used in this case? 谁能描述或引述我从多个线程调用DLL模块的python的描述,以及在这种情况下应如何使用GIL?

How to release GIL when calling a C++ function from Python via Boost.Pyhton: 通过Boost.Pyhton从Python调用C ++函数时如何释放GIL:

http://wiki.python.org/moin/boost.python/HowTo#Multithreading_Support_for_my_function http://wiki.python.org/moin/boost.python/HowTo#Multithreading_Support_for_my_function

The answer is no, the GIL will never truly multi-thread unless the DLL manually releases the lock. 答案是否定的,除非DLL手动释放锁,否则GIL将永远不会真正实现多线程。 Python allows exactly one thread to run at a time unless the extension manually says, "I'm blocked, carry on without me." Python允许一次只运行一个线程,除非扩展程序手动说“我被阻止,没有我继续前进”。 This is commonly done with the Py_BEGIN_ALLOW_THREADS macro (and undone with Py_END_ALLOW_THREADS) defined in python's include/ceval.h. 这通常是通过python的include / ceval.h中定义的Py_BEGIN_ALLOW_THREADS宏(和Py_END_ALLOW_THREADS撤消)来完成的。 Once an extension does this, python will allow another thread to run, and the first thread doing any python stuff will likely cause problems (as the comment question notes.) It's really meant for blocking on I/O or going into heavy compute time. 扩展执行此操作后,python将允许另一个线程运行,并且第一个执行python任务的线程可能会引起问题(如注释问题所述。)这实际上是为了阻塞I / O或占用大量计算时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM