473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

python thread state

hi,

i'm trying to write a multithreaded embedded python application and i'm
having some trouble. i found this article "embedding python in
multi-threaded c/c++ applications" in the python journal
(http://www.linuxjournal.com/article/3641) but there still seems to be a
step missing for me.

each time a function in my c module is called, it's called on a different c
thread. i would then like to call a function in an embedded python script.
from my understanding of the article, you can associate a python script
with a c thread by calling PyThreadState_New as in this code:

// save main thread state
PyThreadState * mainThreadState = NULL;
mainThreadState = PyThreadState_Get();
PyEval_ReleaseLock();

// setup for each thread
PyEval_AcquireLock();
PyInterpreterState * mainInterpreterState = mainThreadState->interp
PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);
PyEval_ReleaseLock();

//execute python code
PyEval_AcquireLock();
PyThreadState_Swap(myThreadState);
# execute python code
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
unfortunately, this doesn't work for me because each time i get called to
execute python code, i'm in a new c thread and PyThreadState_Swap seems to
want to be executed in the same c thread that PyThreadState_New was
executed in. if this isn't the case, please let know.

i then called PyThreadState_New each time i wanted to call a python function
in the script, but PyThreadState_New wipes out, or rather gives you a new
global dictionary, because i lost all my global variables. the article
assumes you have one c thread per python thread state, but i want multiple
c threads per python thread state. Is there a c api function that will
associate a c thread without resetting the global dictionary?

thank you,

bryan

Oct 23 '06 #1
0 1177

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
4
by: Randall Hopper | last post by:
What is the correct way to propagate exceptions from Python callbacks? When I do this: Python -> C++ -> Python Callback (example attached) an exception raised in the callback doesn't make it...
2
by: Ugo Di Girolamo | last post by:
I have the following code, that seems to make sense to me. However, it crashes about 1/3 of the times. My platform is Python 2.4.1 on WXP (I tried the release version from the msi and...
1
by: freesteel | last post by:
I have posted about this problem before. SInce then I found a much better article to help with embedding python in a multithreaded application: http://www.linuxjournal.com/article/3641 I...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
3
by: freesteel | last post by:
/* Is this a bug in Py_NewInterpreter? The function below "MyThread" is instantiated from a windows worker thread, but I don't think that is relevant. (I can try this on a linux box, but I...
9
by: Svein Seldal | last post by:
Hi A couple of mulithreaded C API python questions: I) The PyGILState_Ensure() simply ensures python api call ability, it doesnt actually lock the GIL, right? PyGILState_STATE gstate;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.