473,545 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling python interpreter from a thread created in C

Hello,

What should I do to be able to call Pyhton interpreter from a posix
thread that is created in C?

I have a C-library that creates a thread and then makes callbacks to
user supplied function from that threads context. I'm writing a wrapper
for this library and I need to call python interpreter from that callback.

I'm using Python 2.3 in Linux.

Any help appreciated.

--

-jarppe

If you have the right attitude, interesting problems will find you.
-- Eric Steven Raymond
Jul 18 '05 #1
2 2556
On Tue, 02 Nov 2004 11:50:33 GMT, Jarppe <ja************ *****@gmail.com > wrote:
Hello,

What should I do to be able to call Pyhton interpreter from a posix
thread that is created in C?

I have a C-library that creates a thread and then makes callbacks to
user supplied function from that threads context. I'm writing a wrapper
for this library and I need to call python interpreter from that callback.

I'm using Python 2.3 in Linux.

Any help appreciated.


First, you need to ensure that Python is ready for threading. From the
application's main thread, call PyEval_InitThre ads (if this has already been
called, it simply returns. But if this is the first call, it assumes it is
being called on the application's main thread, so you don't want to call this on
your callback thread).

Then, in your thread's callback, call PyGILState_Ensu re before accessing any
part of the Python API. When you're done working with python, call
PyGILState_Rele ase. See the documentation and example here:

http://www.python.org/dev/doc/devel/api/threads.html

(Note that before the example of using PyGILState_Ensu re, the text refers to
needing an interpreter state. This text is left over from versions before
PyGILState was introduced. Just do what the example shows and you shouldn't
have a problem.)

---
Greg Chapman
Jul 18 '05 #2
Hi,

Greg Chapman wrote:
First, you need to ensure that Python is ready for threading. From the
application's main thread, call PyEval_InitThre ads (if this has already been
called, it simply returns. But if this is the first call, it assumes it is
being called on the application's main thread, so you don't want to call this on
your callback thread).
That's it!!! I didn't realize that and I just keep getting SIGSEGV no
matter what I did. I added call to PyEval_InitThre ads() and it started
to work immediately! Thanks!!!
Then, in your thread's callback, call PyGILState_Ensu re before accessing any
part of the Python API. When you're done working with python, call
PyGILState_Rele ase. See the documentation and example here:

http://www.python.org/dev/doc/devel/api/threads.html

(Note that before the example of using PyGILState_Ensu re,
the text refers to
needing an interpreter state. This text is left over
from versions before
PyGILState was introduced. Just do what the example shows
and you shouldn't have a problem.)


Oh, that's interesting! I used PyEval_AcquireT hread() and
PyEval_ReleaseT hread(). Now I changed to
PyGILState_Ensu re()/PyGILState_Rele ase(), and now the code looks much
simpler. And it works :)

Thanks a lot!!!

--

-jarppe

If you have the right attitude, interesting problems will find you.
-- Eric Steven Raymond
Jul 18 '05 #3

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

Similar topics

7
12502
by: skip | last post by:
This question was posed to me today. Given a C/C++ program we can clearly embed a Python interpreter in it. Is it possible to fire up multiple interpreters in multiple threads? For example: C++ main thread 1 Py_Initialize() thread 2 Py_Initialize()
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.