473,387 Members | 1,553 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,387 software developers and data experts.

Embedded python with threads

nik
hi,

I have a C++ app that loads a python module and calls a function from
it. The call is getting the contents of a list that's global in that module.

However, I'd like the python script to be also running a thread that
fills that global list, but can't figure out how to do it. Essentially
the code looks like;

in the C++ app;

////
Py_Initialize();

// PyImport_ImportModule blocks until the myModule script has run
// through...
PyObject* module = PyImport_ImportModule("myModule");

PyObject* function = PyObject_GetAttrString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFunction(function, "");
// use the stuff in pyList
sleep(15);
}
////

and in the myModule.py;

####
import thread
import time
orderListLock = thread.allocate_lock()

pyList = []

def GetList():
global pyList
tmpList = []
orderListLock.acquire()
tmpList = pyList
orderListLock.release()
return tmpList

def keepFillingListThread():
global pyList
while 1:
orderListLock.acquire()
pyList.append(somestuff)
orderListLock.release()
time.sleep(5)

# the following statement happens when the C++ app imports the module,
# but the thread only lives as long the main python thread (which is
# over straight away on my linux PC)

thread.start_new_thread(keepFillingListThread, ())

####

Has anyone any ideas?

thanks,
nik
Jul 18 '05 #1
4 2346
nik
nik wrote:
hi,

I have a C++ app that loads a python module and calls a function from
it. The call is getting the contents of a list that's global in that
module.

However, I'd like the python script to be also running a thread that
fills that global list, but can't figure out how to do it. Essentially
the code looks like;

in the C++ app;

////
Py_Initialize();

// PyImport_ImportModule blocks until the myModule script has run
// through...
PyObject* module = PyImport_ImportModule("myModule");

PyObject* function = PyObject_GetAttrString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFunction(function, "");
// use the stuff in pyList
sleep(15);
}
////

and in the myModule.py;

####
import thread
import time
orderListLock = thread.allocate_lock()

pyList = []

def GetList():
global pyList
tmpList = []
orderListLock.acquire()
tmpList = pyList
orderListLock.release()
return tmpList

def keepFillingListThread():
global pyList
while 1:
orderListLock.acquire()
pyList.append(somestuff)
orderListLock.release()
time.sleep(5)

# the following statement happens when the C++ app imports the module,
# but the thread only lives as long the main python thread (which is
# over straight away on my linux PC)

thread.start_new_thread(keepFillingListThread, ())

####

Has anyone any ideas?

thanks,
nik


Hi again,

I think I'm one step further. I've put the
thread.start_new_thread(keepFillingListThread, ()) command into its own
function, like

def startThreads():
thread.start_new_thread(keepFillingListThread, ())
while 1:
pass

and then in the C++ part, I've used boost::thread to create a thread to
call startThreads like;

void myBoostThread::operator () ()
{
PyObject* function = PyObject_GetAttrString(m_cfg.module, "createThreads");
PyObject* result = PyObject_CallFunction(function, "");
}

// after the PyObject* module = PyImport_ImportModule("myModule"); line
from above:
myBoostThreadParams params;
params.module = module;
myBoostThread mythread(params);
boost::thread theThread(mythread);

this seems to work (at least, the python threads stay alive), but it
rapidly crashes out with

Fatal Python error: ceval: tstate mix-up
Aborted

as soon as I call the function GetList above.

Can anyone help me?

thanks
Jul 18 '05 #2
On Wed, 15 Sep 2004 18:05:54 +0200, nik <my************@noos.fr> wrote:
////
Py_Initialize();

// PyImport_ImportModule blocks until the myModule script has run
// through...
PyObject* module = PyImport_ImportModule("myModule");

PyObject* function = PyObject_GetAttrString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFunction(function, "");
// use the stuff in pyList
sleep(15);
}
I'm not sure I fully understand your problem, but in the loop above, you should
have:

Py_BEGIN_ALLOW_THREADS
sleep(15);
Py_END_ALLOW_THREADS

This releases the Python GIL during the sleep call, allowing your list-filling
thread to run.

# the following statement happens when the C++ app imports the module,
# but the thread only lives as long the main python thread (which is
# over straight away on my linux PC)


This is what I don't understand, since the Python main thread should be the
thread on which Py_Initialize was called; it should remain alive (from Python's
perspective) until Py_Finalize is called.

---
Greg Chapman

Jul 18 '05 #3
nik
Greg Chapman wrote:
On Wed, 15 Sep 2004 18:05:54 +0200, nik <my************@noos.fr> wrote:

////
Py_Initialize();

// PyImport_ImportModule blocks until the myModule script has run
// through...
PyObject* module = PyImport_ImportModule("myModule");

PyObject* function = PyObject_GetAttrString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFunction(function, "");
// use the stuff in pyList
sleep(15);
}

I'm not sure I fully understand your problem, but in the loop above, you should
have:

Py_BEGIN_ALLOW_THREADS
sleep(15);
Py_END_ALLOW_THREADS

This releases the Python GIL during the sleep call, allowing your list-filling
thread to run.

# the following statement happens when the C++ app imports the module,
# but the thread only lives as long the main python thread (which is
# over straight away on my linux PC)

This is what I don't understand, since the Python main thread should be the
thread on which Py_Initialize was called; it should remain alive (from Python's
perspective) until Py_Finalize is called.


I think I was making an assumption that since the list filling threads
didn't run that the import module was calling the create thread command,
then exiting (killing the children threads along with it) - I'm a novice
at threads with python, so I'm probably misunderstanding the general
picture. However, maybe your suggestion of the Py_BEGIN_ALLOW_THREADS
will make the difference. I'll try again as soon as I can (not until
next week now)...

many thanks for the reply,
nik
---
Greg Chapman

Jul 18 '05 #4
On Fri, 17 Sep 2004 01:05:13 +0200, nik <my************@noos.fr> wrote:
I think I was making an assumption that since the list filling threads
didn't run that the import module was calling the create thread command,
then exiting (killing the children threads along with it) - I'm a novice
at threads with python, so I'm probably misunderstanding the general
picture. However, maybe your suggestion of the Py_BEGIN_ALLOW_THREADS
will make the difference. I'll try again as soon as I can (not until
next week now)...


OK, this sounds like you were definitely not releasing the GIL (the Global
Interpreter Lock). To get an idea of how Python's threading works (from the C
side of things), I'd suggest reading this:

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

That's from the documentation for 2.4, but I believe everything there applies to
2.3 as well. I'm linking to it because it includes some discussion of the
PyGILState functions (this discussion is missing from the 2.3 docs, but the
functions are there).

---
Greg Chapman

Jul 18 '05 #5

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

Similar topics

0
by: Wolfgang | last post by:
I have a problem with linking my CPP Code under a irix6 machine (sgi, UNIX). In my CPP code I use some Functions which are written in Python. So its a kind of CPP wrapper for my Python functions In...
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...
4
by: Dennis Clark | last post by:
Hi all, I've looked through the threads about embedded Python that are a year and a half old, and I thought that I'd ask this question now to see if anything has changed. Has anyone, or is...
1
by: amit | last post by:
Hello, I am embedding a python script in a C++ application. The script can be called simultaneously from multiple threads. What is the correct way to implement this situation: 1) Have...
2
by: fotis | last post by:
hello there! I am playing with embedded python these days. I wrote sth like this: ------------------------------ Code ------------------------------- #include <Python.h> #include <iostream>...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
0
by: Dirk Runge | last post by:
Hi! I have embedded Python in an C++ App. The Python-Interpreter is running in its own Thread (I'm using PThreads). I use PyRun_SimpleString to run Python-Code that the user entered in an...
3
by: Malte Forkel | last post by:
I would like to use Python on a router, an Edimax BR-6104K, running OpenWrt (http://www.openwrt.org). While I probably won't need most of the fancier stuff in Python, serial I/O and threads should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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,...

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.