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 4 2379
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Linux with the same code. Under the sgi, UNIX machine some errors
occur and I don't no why....
|
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).
|
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 work with, I'm just running a Linux 2.4 kernel
on an ARM9 platform.
|
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.
|
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)
| |
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 not seem to be
necessary.
<quote>
In the default implementation of threaded applications against...
|
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).
|
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 probably a litte to big.
Any suggestions?
Thanks in advance,
Malte
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |