473,574 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Import Module blocks until the myModule script has run
// through...
PyObject* module = PyImport_Import Module("myModul e");

PyObject* function = PyObject_GetAtt rString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFu nction(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.a cquire()
tmpList = pyList
orderListLock.r elease()
return tmpList

def keepFillingList Thread():
global pyList
while 1:
orderListLock.a cquire()
pyList.append(s omestuff)
orderListLock.r elease()
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_ne w_thread(keepFi llingListThread , ())

####

Has anyone any ideas?

thanks,
nik
Jul 18 '05 #1
4 2361
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_Import Module blocks until the myModule script has run
// through...
PyObject* module = PyImport_Import Module("myModul e");

PyObject* function = PyObject_GetAtt rString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFu nction(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.a cquire()
tmpList = pyList
orderListLock.r elease()
return tmpList

def keepFillingList Thread():
global pyList
while 1:
orderListLock.a cquire()
pyList.append(s omestuff)
orderListLock.r elease()
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_ne w_thread(keepFi llingListThread , ())

####

Has anyone any ideas?

thanks,
nik


Hi again,

I think I'm one step further. I've put the
thread.start_ne w_thread(keepFi llingListThread , ()) command into its own
function, like

def startThreads():
thread.start_ne w_thread(keepFi llingListThread , ())
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_GetAtt rString(m_cfg.m odule, "createThreads" );
PyObject* result = PyObject_CallFu nction(function , "");
}

// after the PyObject* module = PyImport_Import Module("myModul e"); line
from above:
myBoostThreadPa rams params;
params.module = module;
myBoostThread mythread(params );
boost::thread theThread(mythr ead);

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_Import Module blocks until the myModule script has run
// through...
PyObject* module = PyImport_Import Module("myModul e");

PyObject* function = PyObject_GetAtt rString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFu nction(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_TH READS

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_Import Module blocks until the myModule script has run
// through...
PyObject* module = PyImport_Import Module("myModul e");

PyObject* function = PyObject_GetAtt rString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFu nction(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_TH READS

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 misunderstandin g 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 misunderstandin g 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
1709
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 my Python Code I use threads to communicate over the network and stuff like this. Compilation and linking are working very well under Windows and...
2
2757
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 the debug version built by me, both downloaded today to have the latest version).
4
3034
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 anyone working with Python in an embedded Linux environment? Mine is NO where near as constrained as a cell phone since I've got plenty of memory to...
1
3698
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 unique python interpreter instantiations ( Py_Initialize() ) for each thread.
2
2518
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> #include <cmath> /* Return the square root of an argument */ static PyObject* Fotis_root(PyObject *self, PyObject *args)
11
4238
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do...
0
1765
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 editorwindow. I want the user to be able to stop the execution of Python-Code (e.g. using a Cancel-Button).
3
1831
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 be supported. The router is based on the ADM5120P, has 2MB of flash and 16MB of RAM, so the version of Python 2.5.1 that is part of OpenWrt is...
0
7801
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
7719
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
8044
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
8096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5616
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
5299
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
3739
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.