This is an OpenPGP/MIME signed message (RFC 4880 and 3156) -----------------------9e29034e00ea96b7d10a477458ea4ed4 Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;charset=utf-8
Hi
There was a bug in the example code. I fixed it and it STILL deadlocks (my larger software project deadlocks when I call python from C++).
Updated code:
/* * This code deadlocks on python3-dev 3.12.3 (ubuntu 24.04 lts)
*
* g++ python_thread_test.cpp `python3-config --cflags --libs --embed`
* ./a.out
*
* uname:
* Linux softice 6.8.0-36-generic SMP PREEMPT_DYNAMIC x86_64 GNU/Linux
*/
#include <Python.h>
#include <thread>
#include <vector>
#include <iostream>
void thread_function() {
// Ensure this thread has the GIL
PyGILState_STATE gstate = PyGILState_Ensure();
// Execute some Python code
PyRun_SimpleString("print('Hello from std::thread!')");
// Release the GIL
PyGILState_Release(gstate);
}
int main() {
// Initialize the Python Interpreter
Py_Initialize();
// Create a vector of threads
std::vector<std::thread> threads;
// Launch threads
for (int i = 0; i < 5; ++i) {
threads.push_back(std::thread(thread_function));
}
// Join threads
for (auto& t : threads) {
t.join();
}
// Finalize the Python Interpreter
Py_Finalize();
return 0;
}
sunnuntaina 7. heinäkuuta 2024 klo 10:24 ip, Tomas Ukkonen <
[email protected]> kirjoitti:
Hello
Is this python c api bug? The following C++ code (g++) deadlocks on Ubuntu Linux.
/* * This code deadlocks on python3-dev 3.12.3 (ubuntu 24.04 lts)
*
* g++ python_thread_test.cpp `python3-config --cflags --libs --embed`
* ./a.out
*
* uname:
* Linux softice 6.8.0-36-generic SMP PREEMPT_DYNAMIC x86_64 GNU/Linux
*/
#include <Python.h>
#include <thread>
#include <vector>
#include <iostream>
void perform_long_operation() {
// Simulate a long-running task
std::this_thread::sleep_for(std::chrono::seconds(5));
}
void thread_function() {
// Ensure this thread has the GIL
PyGILState_STATE gstate = PyGILState_Ensure();
// Execute some Python code
PyRun_SimpleString("print('Hello from std::thread!')");
// Release the GIL for long operation
Py_BEGIN_ALLOW_THREADS
perform_long_operation();
Py_END_ALLOW_THREADS
// Re-acquire the GIL and execute more Python code
gstate = PyGILState_Ensure();
PyRun_SimpleString("print('Thread operation completed!')");
// Release the GIL
PyGILState_Release(gstate);
}
int main() {
// Initialize the Python Interpreter
Py_Initialize();
// Create a vector of threads
std::vector<std::thread> threads;
// Launch threads
for (int i = 0; i < 5; ++i) {
threads.push_back(std::thread(thread_function));
}
// Join threads
for (auto& t : threads) {
t.join();
}
// Finalize the Python Interpreter
Py_Finalize();
return 0;
}
Tomas Ukkonen
-----------------------9e29034e00ea96b7d10a477458ea4ed4--
-----BEGIN PGP SIGNATURE-----
Version: ProtonMail
wsBzBAEBCAAnBYJmiu86CZCaEYwMt4b97BYhBJbmzyegv3Wo8O5gpZoRjAy3 hv3sAAAGZQgAry3/hOkYMNAsE4FgfSHzeGmxt9xdeejo6X8TYT+6SqxtPxZw cIdIBxG2HodcJ625hz0Ouj0Ft+TZZOhwOrk6uPZUsya0Q1CoMfALl4jpaigh k2jB5alPivARIrTbg0jreVhrXd+HjSmIie0Y3j1e5Vg2v6u+z+hOH2JXhlyx +CFwCvBdKTERIy5HaT2CQNBBBpt/AOUjT2L4v333qOBqcu7BHihN+v5cMQe0 RyzOeA/W7yVpB9d89CedS78PnrCu86Hgky1zisWJ2TBw4gomZcF1hdJnwNuQ LrK3piMdNfCo2Ib08Hc7cVDy58/L7PdUXuUf8QTigw7m7QLWOpJKIQ==
=7W+t
-----END PGP SIGNATURE-----
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)