Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True multithreading with boost.python

I'm trying to test a multi-threaded C++ DLL. This DLL is supposed to be thread-safe. 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. 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.

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?

like image 338
jspencer Avatar asked Dec 12 '11 20:12

jspencer


People also ask

Does Python support true multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.

Does multithreading in Python improve performance?

This is why Python multithreading can provide a large speed increase. The processor can switch between the threads whenever one of them is ready to do some work. Using the threading module in Python or any other interpreted language with a GIL can actually result in reduced performance.

How do I start multiple threads at the same time in Python?

To implement threading in Python, you have to perform three steps: Inherit the class that contains the function you want to run in a separate thread by using the Thread class. Name the function you want to execute in a thread run() . Call the start() function from the object of the class containing the run() method.

Which is better multiprocessing or multithreading in Python?

The short answer is: Multithreading for I/O intensive tasks and; Multiprocessing for CPU intensive tasks (if you have multiple cores available)


2 Answers

How to release GIL when calling a C++ function from Python via Boost.Pyhton:

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

like image 114
lvella Avatar answered Sep 18 '22 08:09

lvella


The answer is no, the GIL will never truly multi-thread unless the DLL manually releases the lock. Python allows exactly one thread to run at a time unless the extension manually says, "I'm blocked, carry on without me." 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. 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.

like image 32
jspencer Avatar answered Sep 19 '22 08:09

jspencer